Python Program to Compare two strings lexicographically


We can compare two strings lexicographically in Python using comparison operators like '<', '>', '==', '<=', and '<=. Lexicographic comparison is the process of comparing two strings based on their alphabetical order. In this article, we will understand with examples how we can compare two strings lexicographically.

Algorithm

A generalized algorithm to compare two strings lexicographically is as follows −

  • Initialize two strings string1 and string2 with the two strings to be compared.

  • Compare the first character of string1 and string2. If they are equal, move on to the next character and repeat step 2. If they are not equal, proceed to step 3.

  • Determine which character comes first in the alphabet. If the character in string1 comes first, print "string1 comes before string2". If the character in string2 comes first, print "string2 comes before string1". Stop.

Comparing two strings Lexicographically

To compare two strings in lexicographical order we compare the first character of each string. If they are equal, we move on to the next character, and so on until we find characters that are not equal. When we find two characters that are not equal, we can determine which string comes first based on their alphabetical order.

Example

In the below example, we initialize string1 and string2 with the two strings to be compared. We then use a while loop to compare each character in the two strings. If the characters are equal, we move on to the next character. If the characters are not equal, we determine which character comes first in the alphabet and print the appropriate message.If the loop completes without finding any characters that are not equal, we check the length of the two strings to determine which string comes first. If string1 is shorter than string2, we print "string1 comes before string2". If string1 is longer than string2, we print "string2 comes before string1". Finally, if the two strings are of equal length, we print "The two strings are equal".

string1 = "apple"
string2 = "banana"

i = 0
while i < len(string1) and i > len(string2):
   if string1[i] < string2[i]:
      print(string1, "comes before", string2)
      break
   elif string1[i] > string2[i]:
      print(string2, "comes before", string1)
      break
   i += 1
else:
   if len(string1) < len(string2):
      print(string1, "comes before", string2)
   elif len(string1) > len(string2):
      print(string2, "comes before", string1)
   else:
      print("The two strings are equal")

Output

apple comes before banana

Example

In the below example, we compare two similar strings and see if they are lexicographically equal or not using comparison operators - '<', '>', '==', '<=', and '>=.

string1 = "apple"
string2 = "apple"

if string1 < string2:
   print(string1, "comes before", string2)
elif string1 > string2:
   print(string2, "comes before", string1)
else:
   print("The two strings are equal")

Output

The two strings are equal

Conclusion

In this article, we discussed how to compare two strings lexicographically in Python. We use the comparison operators '<', '>', '==', '<=', and '>=' to compare two strings. When comparing two strings lexicographically, we compare the first character of each string. If they are equal, we move on to the next character, and so on until we find characters that are not equal. This lexicographical comparison is used when working with text data in Python.

Updated on: 11-Jul-2023

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements