- String comparison is performed using the characters in both strings.
- The characters in both strings are compared one by one. When different characters are found then their Unicode value is compared.
- The character with lower Unicode value is considered to be smaller so all the uppercase letters come before all the lowercase letters.
Example :-
fruit1 = 'Apple'
print(fruit1 == 'Apple')
print(fruit1 != 'Apple')
print(fruit1 < 'Apple')
print(fruit1 > 'Apple')
print(fruit1 <= 'Apple')
print(fruit1 >= 'Apple')
Output:
True
False
False
False
True
True
Example :-
fruit1 = input('Please enter the name of first fruit:\n')
fruit2 = input('Please enter the name of second fruit:\n')
if fruit1 < fruit2:
print(fruit1 + " comes before " + fruit2 + " in the dictionary.")
elif fruit1 > fruit2:
print(fruit1 + " comes after " + fruit2 + " in the dictionary.")
else:
print(fruit1 + " and " + fruit2 + " are same.")
Output:
Please enter the name of first fruit:
Apple
Please enter the name of second fruit:
Banana
Apple comes before Banana in the dictionary.
Please enter the name of first fruit:
Orange
Please enter the name of second fruit:
Orange
Orange and Orange are same.
Example :-
(
'apple'==
'Apple')
(
'apple'>
'Apple')
(
'A unicode is',
ord(
'A'),
',a unicode is',
ord(
'a'))
Output:
False
True
A unicode
is65
,a unicode
is97
ord() and chr() Functions
ord()
- function returns the ASCII code or Unicode code of the character.
chr()
- function returns character represented by a ASCII number.
Example :-
ch = 'b'
print (ord(ch))
print (chr(97))
print (ord('A'))
output:-
98
a
65
In python string can compare by various methods which are :-
Method 1: Using Relational Operators
The relational operators compare the Unicode values of the characters of the strings from the zeroth index till the end of the string. It then returns a Boolean value according to the operator used.
This is done using the following operators:
- ==: This checks whether two strings are equal
- !=: This checks if two strings are not equal
- <: This checks if the string on its left is smaller than that on its right
- <=: This checks if the string on its left is smaller than or equal to that on its right
- >: This checks if the string on its left is greater than that on its right
- >=: This checks if the string on its left is greater than or equal to that on its right
print("Abcd" == "Abcd") print("Abcd " < "abcd") print("Abcd " > "abcd") print("Abcd " != " Abcd ") |
Output:
True
True
False
False
Method 2: Using is and is not
is operator checks whether both the operands refer to the same object or not.
The same is the case for != and is not.
Example :-
str1 = " Abcd " str2 = " Abcd " str3 = str1 print("ID of str1 =", hex(id(str1))) print("ID of str2 =", hex(id(str2))) print("ID of str3 =", hex(id(str3))) print(str1 is str1) print(str1 is str2) print(str1 is str3) str1 += "s" str4 = " Abcde" print("\nID of changed str1 =", hex(id(str1))) print("ID of str4 =", hex(id(str4))) print(str1 is str4) |
Output:
ID of str1 = 0x7f6037051570
ID of str2 = 0x7f6037051570
ID of str3 = 0x7f6037051570
True
True
True
ID of changed str1 = 0x7f60356137d8
ID of str4 = 0x7f60356137a0
False
The object ID of the strings may vary on different machines. The object IDs of str1, str2 and str3 were the same therefore they the result is True in all the cases. After the object id of str1 is changed, the result of str1 and str2 will be false. Even after creating str4 with the same contents as in the new str1, the answer will be false as their object IDs are different.
Vice-versa will happen with is not.
Method 3: Creating a user-defined function.
To compare two strings according to some other parameters, we can make user-defined functions based upon the number of digits.
Example :-
# function to compare string # based on the number of digits def compare_strings(str1, str2): count1 = 0 count2 = 0 for i in range(len(str1)): if str1[i] >= "0" and str1[i] <= "9": count1 += 1 for i in range(len(str2)): if str2[i] >= "0" and str2[i] <= "9": count2 += 1 return count1 == count2 print(compare_strings("123", "12345")) print(compare_strings("12345", "geeks")) print(compare_strings("12geeks", "geeks12")) |
Output:
False
False
True
Method 4: Using the difflib
module
Python also offers a way to compare multi-line strings, and entire lists of words.
The output can be configured according to various formats of diff tools.
Example:-
import difflib
# define original text
# taken from: https://en.wikipedia.org/wiki/Internet_Information_Services
original = ["About the IIS", "", "IIS 8.5 has several improvements related", "to performance in large-scale scenarios, such", "as those used by commercial hosting providers and Microsoft's", "own cloud offerings."]
# define modified text
edited = ["About the IIS", "", "It has several improvements related", "to performance in large-scale scenarios."]
# initiate the Differ object
d = difflib.Differ()
# calculate the difference between the two texts
diff = d.compare(original, edited)
# output the result
print ('\n'.join(diff))
output:-
About the IIS
- IIS 8.5 has several improvements related
? ^^^^^^
+ It has several improvements related
? ^
- to performance in large-scale scenarios, such
? ^^^^^^
+ to performance in large-scale scenarios.
? ^
- as those used by commercial hosting providers and Microsoft's
- own cloud offerings.
In this script Lines with deletions are indicated by -
signs whereas lines with additions start with a +
sign. Furthermore, lines with changes start with a question mark. Changes are indicated using ^
signs at the according position. Lines without an indicator are still the same.
================================================================
0 Comments