Hướng dẫn compare alphanumeric strings python

In this article, we will learn what is strings in a programming language, how to create them, and their uses. Further, we will study various operators to compare strings in python. At last, we will study some Python strings comparison in brief along with its python code example and output. So, let’s get started!

Nội dung chính

  • What are Strings?
  • Python String Comparison operators
  • String Equals Check in Python
  • What about Case insensitive comparisons?
  • Python String Comparison
  • Can you use == to compare strings?
  • How can we compare two strings in Python?
  • What does == compare in Python?
  • How do you compare strings in if condition in Python?

Nội dung chính

  • What are Strings?
  • Python String Comparison operators
  • String Equals Check in Python
  • What about Case insensitive comparisons?
  • Python String Comparison
  • Can you use == to compare strings?
  • How can we compare two strings in Python?
  • What does == compare in Python?
  • How do you compare strings in if condition in Python?

What are Strings?

A string is generally a sequence of characters. A character is a simple symbol. For example, in the English Language, we have 26 characters available. The computer system does not understand characters and hence, therefore, deal with binary numbers. Even though we can see characters on our monitor screens, but internally it is stored and manipulated as a combination of 0s and 1s. The conversion of characters and the binary number is called encoding, and the reverse of this is known as decoding.  Some of the popular encodings are ASCII and Unicode. In the Python programming language, a string is a sequence of Unicode characters.

Python String Comparison operators

In python language, we can compare two strings such as identify whether the two strings are equivalent to each other or not, or even which string is greater or smaller than each other. Let us check some of the string comparison operator used for this purpose below:

  • ==: This operator checks whether two strings are equal.
  • !=: This operator checks whether two strings are not equal.
  • =: This operator checks whether the string on the left side is greater than the string on the right side.

String Equals Check in Python

In python programming we can check whether strings are equal or not using the “==” or by using the “.__eq__” function.

Example:

s1 = 'String'
s2 = 'String'
s3 = 'string'

# case sensitive equals check
if s1 == s2:
    print['s1 and s2 are equal.']

if s1.__eq__[s2]:
    print['s1 and s2 are equal.']

Here, we check string s1 and s2 whether they are equal or not, and then use the “if” conditional statement with a combination of the equal operator.

The output of the above code is as given below:

 s1 and s2 are equal.

 s1 and s2 are equal.

What about Case insensitive comparisons?

While checking the equality in strings sometimes we wish to ignore the case of the string while comparison. So, as a solution to this, we can use the case fold[], lower[], or upper[] function for ignoring the case insensitive comparison of string equality.

s1 = 'String'
s2 = 'String'
s3 = 'string'

if s1.casefold[] == s3.casefold[]:
    print[s1.casefold[]]
    print[s3.casefold[]]
    print['s1 and s3 are equal in case-insensitive comparison']

if s1.lower[] == s3.lower[]:
    print[s1.lower[]]
    print[s3.lower[]]
    print['s1 and s3 are equal in case-insensitive comparison']

if s1.upper[] == s3.upper[]:
    print[s1.upper[]]
    print[s3.upper[]]
    print['s1 and s3 are equal in case-insensitive comparison']

The output of the above code is as given below:

 string

 string

 s1 and s3 are equal in case-insensitive comparison

 string

 string

 s1 and s3 are equal in case-insensitive comparison

 STRING

 STRING

 s1 and s3 are equal in case-insensitive comparison

Conclusion

So, in this article, we studied how to compare strings in a python programming language. Also, we studied some string comparison operators and check string equality. Even we checked the string case insensitive comparison.

Python String comparison can be performed using equality [==] and comparison [, !=, =] operators. There are no special methods to compare two strings.

Python String Comparison

Python 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. Let’s look through some examples for string comparison.

fruit1 = 'Apple'

print[fruit1 == 'Apple']
print[fruit1 != 'Apple']
print[fruit1 < 'Apple']
print[fruit1 > 'Apple']
print[fruit1 = 'Apple']

Output:

True
False
False
False
True
True

Both the strings are exactly the same, hence they are equal. So equality operator is returning True in this case. Let’s look at another example where we will get inputs from the user and then compare them.

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.

Let’s see if the comparison is case sensitive or not? Also if ‘a’ comes ‘A’?

print['apple' == 'Apple']
print['apple' > 'Apple']
print['A unicode is', ord['A'], ',a unicode is', ord['a']]

Output:

False
True
A unicode is 65 ,a unicode is 97

So “Apple” is smaller when compared to “apple” because of their Unicode values. We are using ord[] function to print the Unicode code point value of the characters. What if one of the string is made of second string and some additional characters?

print['Apple' < 'ApplePie']

Output: True If the characters sequence are the same in both the strings but one of them have some additional characters, then the larger length string is considered greater than the other one. What if we use < and > operators to compare two equal strings?

print['apple' < 'apple']
print['apple' > 'apple']

Output:

False
False

Obviously, both the strings are neither smaller nor greater than the other one. Hence the output is false in both the cases.

You can checkout complete python script and more Python examples from our GitHub Repository.

Can you use == to compare strings?

You should not use == [equality operator] to compare these strings because they compare the reference of the string, i.e. whether they are the same object or not. On the other hand, equals[] method compares whether the value of the strings is equal, and not the object itself.

How can we compare two strings in Python?

Python String comparison can be performed using equality [==] and comparison [, != , =] operators. There are no special methods to compare two strings.

What does == compare in Python?

The == operator compares the value or equality of two objects, whereas the Python is operator checks whether two variables point to the same object in memory. In the vast majority of cases, this means you should use the equality operators == and != , except when you're comparing to None .

How do you compare strings in if condition in Python?

How to Compare Strings Using the

Chủ Đề