Python String Basics
In Python, strings are objects that contain sequences of character data; they are implemented as a byte array, which translates to Unicode. Python strings are immutable, which means that once created, the string cannot be changed. Strings management functions always make a new line instead of modifying an existing one. Python provides essential functions for searching, concatenating, reversing, splitting, and comparing strings. Python strings are not mutable; once created, they cannot be changed. The string manipulation functions always create a new string instead of modifying the existing one. Python string literals can be enclosed in single, double, or triple quotes. However, single quotes are more commonly used. With triple quotes, strings can span multiple lines without using an escape character. Long lines can be split over multiple lines by adding a backslash ("\") at the end of each line. To create a multiline string, enclose the text in triple quotes ( """ or '''). Characters in a string can be accessed using the zero-based syntax .
In Python, strings are an array of 8-bit (ASCII) or 16-bit (Unicode) bytes, where each character in the string is represented by one byte. Unicode strings can contain a more comprehensive range of characters and support more than standard strings, such as Chinese and Japanese characters. In Python 2, you must start Unicode strings with the "u" character. Python 3 uses Unicode strings by default.
my_string = '"Double quotes" example' print(my_string) # output: "Double quotes" example
my_string = 'Backslash\r\nExample' print(my_string) # output: Backslash # output: Example
my_string = 'Line 1'\ 'Line 2'\ 'Line 3' print(my_string) # output: Line 1Line 2Line 3
my_string = """Line 1 Line 2 Line 3""" print(my_string) # output: Line 1 # output: Line 2 # output: Line 3
Comparing Python strings using the == and != operators
We can use the boolean operators "==" and "! =" to compare two strings. We can use the "==" operator to test strings for similarity and the "!=" operator to check strings for inconsistencies. Each of the operators will return a boolean value True or False, depending on the result.
user_input = 'qwerty' password = 'qwerty' print(password == user_input) # output: True
String comparison is case sensitive
The string comparison is case sensitive, which means that the strings "abc" and "ABC" are not equal. This is because the lowercase and uppercase characters have different ASCII codes.
str_1 = 'qwerty' str_2 = 'QWERTY' print(str_1 == str_2) # output: False
To compare two Python strings containing characters in different cases, you can use the lower() or upper() string methods. To perform a case-insensitive comparison, you need to convert both strings to the same case and compare.
str_1 = 'qwerty' str_2 = 'QWERTY' print(str_1.lower() == str_2.lower()) # output: True
Comparing Python strings using the 'is' and 'is not' operators
You can also use the 'is' and 'is not' operators to compare strings. Unlike '==' and '!=', the comparison 'is' performed on the string id. To find out the string id, use the id() method. Usually, the is operator 'is' used to compare objects in Python, but you can compare strings in the same way.
str_1 = 'qwerty' str_2 = 'QWERTY' print(str_1 is str_2) # output: False
Сompare Python strings conditions <, >, <=, >=
Between strings in Python, you can perform <, or > comparisons as well as between numbers. If the first line is a prefix of the second, then it is less than the second. If two lines start the same, but then differ, then less is the line with the first differing character less. This sorting order is called lexicographic (the same is used in dictionaries).
print('aaa' < 'aab') # output: True
In Python, you can make multiple string comparisons at the same time.
print('aba' < 'baba' < 'cada'< 'daca') # output: True
Python provides a rich set of methods for comparing strings. If you want to check for string equality, use the '==' or '!=' operators. If you want to compare strings as objects, use the 'is' and 'is not' operators. If you want to know which string is lexicographically smallest, use the '<', '>', '<=', '>=' operators.