What are strings in Python?
Internally, Python stores strings as an array of 16-bit Unicode bytes (or 8-bit ASCII bytes for Python 2), where each character in the string is represented by one byte. Python strings are immutable, which means they cannot be changed once created. All string processing methods return a copy of the string and do not modify the original. Python doesn't have a separate data type for a single character - it's just a string of length 1.
The built-in "str" library provides an extensive list of methods for searching, concatenating, reversing, splitting, comparing strings, and more.
String literals can be enclosed in single, double, or triple quotes. Single quotes are most commonly used, and you can use single quotes in double-quoted strings and vice versa. Triple quotes are frequently used to represent multiline strings and docstrings.
Comparing Python strings using the == and != operators
You can use the boolean operators "==" and "! =" to compare two strings. You 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 = 'apple' password = 'apple' print(password == user_input) # True
user_input = 'apple' password = 'mango' print(password != user_input) # 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 = 'banana' str_2 = 'BANANA' print(str_1 is str_2) # False
str_1 = 'banana' str_2 = 'BANANA' print(str_1 is not str_2) # True
С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') # True
In Python, you can make multiple string comparisons at the same time.
print('123' < '2123' < '3123' < '4123') # True
How to compare strings in a case-insensitive manner?
To perform a case-insensitive string comparison, you must first convert both strings to lower or upper case using the lower() or upper() string methods and then perform the string comparison. This is because lowercase and uppercase characters have different ASCII codes.
str_1 = 'apple' str_2 = 'APPLE' print(str_1 == str_2) # False print(str_1.lower() == str_2.lower()) # True
str_1 = 'cherry' str_2 = 'CHERRY' print(str_1 == str_2) # False print(str_1.upper() == str_2.upper()) # 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. To perform a case-insensitive string comparison, convert both strings to lowercase before comparing.