Strings in Python
In Python, strings are represented in two ways: basic strings and Unicode strings.
Basic Python strings are an array of 8-bit ASCII bytes. Each byte represents a specific character in the alphabet. The basic Python strings work well with ASCII strings (without special characters).
basic_string = 'I am a basic string'
Unicode strings are represented as an array of 16-bit ASCII bytes and provide more flexibility when working with extended alphabets containing special characters. In Unicode strings, you can use non-ASCII characters. To create a Unicode string, use the "u" prefix before the beginning of the string.
unicode_string = u'I am an Unicode string'
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.