How to compare two strings lexicographically in python

Strings in Python are compared with == and != operators. These compare if two Python strings are equivalent or not equivalent, respectively. They return True or False .

Often, when you’re working with strings in Python, you may want to compare them to each other. For example, you may want to compare a user’s email address against the one you have stored in a database when you are asking them to reset their password.

Python includes a number of comparison operators that can be used to compare strings. These operators allow you to check how strings compare to each other, and return a True or False value based on the outcome.

This tutorial will discuss the comparison operators available for comparing strings in Python. We’ll walk through an example of each of these operators to show how they work, and how you can use them in your code. If you’re looking to learn how to compare strings in Python, this article is for you.

Python String is and is Not Equal To

Strings are sequences of characters that can include numbers, letters, symbols, and whitespaces. Strings are an important data type because they allow coders to interact with text-based data in their programs.

When you’re working with a string, you may want to see whether a string is or is not equal to another string. That’s where the == and != string comparison operators come in.

The == equality operator returns True if two values match; otherwise, the operator returns False. The != operator returns True if two values do not match, and False if two values match.

It’s important to note that string comparisons are case sensitive . So, lowercase letters and uppercase letters will affect the result of the comparisons you perform in your Python program.

Let’s say that you are building a game that tests players on their knowledge of state capitals. In order to earn points, players must correctly answer a question. So, a player may be given the state California, and in order to gain points, they would need to enter that the capital is Sacramento into the program.

Here’s an example of this guessing game application that compares a user’s answer to the answer stored by the program:

random_state = "Delaware" message = "What is the capital of ", random_state user_answer = input(message) state_capital = "Dover" if user_answer == state_capital: print("You are correct!") else: print("The capital of ", random_state, "is", state_capital)

Here’s what happens when we run our guessing game and correctly guess the state capital of Delaware is Dover:

What is the capital of Delaware Dover You are correct!

Our strings are equal, so our if statement evaluates to correct and prints out You are correct! . If we incorrectly guess the state capital is Denver, our code would return:

What is the capital of Delaware Denver The capital of Delaware of Dover

Let’s break down our code. On the first one, we declare our random state, which in this case is Delaware. Then, we use the user input() method to ask the user What is the capital of Delaware .

Our program then declares the state capital is Dover, and uses an if statement to compare whether the state capital the program has stored is equal to what the user has entered.

How to compare two strings lexicographically in python

"Career Karma entered my life when I needed it most and quickly helped me match with a bootcamp. Two months after graduating, I found my dream job that aligned with my values and goals in life!"

Venus, Software Engineer at Rockbot

When we entered Dover , the if statement evaluated to True, so our program printed the message You are correct! to the console. When we entered Denver , our statement evaluated to False, so our program executed the code in the else print statement.

Python is Operator

The most common method used to compare strings is to use the == and the != operators, which compares variables based on their values. However, if you want to compare whether two object instances are the same based on their object IDs, you may instead want to use is and is not .

The difference between == and is (and != and is not ) is that the == comparison operator compares two variables based on their actual value, and the is keyword compares two variables based on their object ids.

Let’s use an example. Say that we have the scores of two users stored as a string, and we want to see whether or not they are the same. We could do so using the following code:

player_one_score = "100" player_two_score = "100" if player_one_score is player_two_score: print("Player #1 and #2 have the same number of points.") else: print("Player #1 and #2 do not have the same number of points.")

Our code returns:

Player #1 and #2 have the same number of points.

In the above code, we could also have used the == operator. However, we used the is operator instead because it uses up less memory and we only needed to compare two objects.

The statement player_one_score is player_two_score evaluated to True in our program because both variables player_one_score and player_two_score have the same object IDs. We can check these IDs by using the id keyword:

print(id(player_one_score)) print(id(player_two_score))

Our code returns:

140239618130992 140239618130992

As you can see, our objects are the same, and so the is operator evaluated to True. Generally, you should use == when you’re comparing immutable data types like strings and numbers, and is when comparing objects.

Python Other Comparison Operators

In addition, you can compare strings in lexicographic order using Python. Lexicographic order refers to ordering letters based on the alphabetical order of their component letters. To do so, we can use the other comparison operators offered by Python. These are as follows:

  • < – Less than
  • > – Greater than
  • <= – Less than or equal to
  • >= – Greater than or equal to

Let’s say we were creating a program that takes in two student names and returns a message with whose name comes first in the alphabet.

We could use the following code to accomplish this task:

student_one = "Penny" student_two = "Paul" if student_one > student_two: print("Penny comes before Paul in the alphabet.") elif student_one < student_two: print("Paul comes before Penny in the alphabet.")

Our code returns:

Paul comes before Penny in the alphabet.

Let’s break down our code. On the first two lines, we declare two variables that store our student names. In this case, these names are Penny and Paul.

Then, we create an if statement that uses the greater than operator to determine whether Penny’s name comes before Paul’s name in lexicographic order. If this evaluates to True, a message is printed to the console telling us that Penny comes before Paul in the alphabet.

We also create an elif statement that uses the less than operator to determine whether Penny’s name comes before Paul’s name in the alphabet. If this evaluates to True, a message is printed to the console telling the user that Paul comes before Penny in the alphabet.

In this case, Paul’s name comes before Penny’s in the alphabet, so the code in our elif block evaluates to true, and the message Paul comes before Penny in the alphabet . is printed to the console.

Conclusion

Comparing two strings is an important feature of Python. For instance, you may be creating a login form that needs to compare the password a user has entered with the password they have set for their account.

Python comparison operators can be used to compare strings in Python. These operators are: equal to ( == ), not equal to ( != ), greater than ( > ), less than ( < ), less than or equal to ( <= ), and greater than or equal to ( >= ). This tutorial explored how these operators can be used to compare strings, and walked through a few examples of string comparison in Python.

You’re now ready to start comparing strings in Python like a pro!

Related Searches: python compare strings ignore case, python compare two strings character by character, python compare two strings for similarity, python compare two strings and return the difference, python compare strings ignore whitespace, python compare strings not equal, python compare strings with wildcard, python compare strings in list, python compare strings lexicographically, python compare strings in two lists

Introduction to Python compare strings

In python, everything that we write inside double or single quotation marks is treated as a string. Strings are immutable sequences of data type and strings are actually a sequence of Unicode characters. In this tutorial, we will talk about python string comparisons. We will take different cases and see how we can compare strings by taking examples using different comparison operators. Moreover, we will also compare strings in the list as well. By the end of this tutorial, you will have a solid knowledge of python compare strings.

ALSO READ: Python pass statement Explained [Beginners Guide]

Example of Python strings

As we already discussed, that string is an immutable sequence data type which is wrapped inside single, double, or triple quotes.

See the example below:

# creating string x = "My name" # printing type print(type(x))

Output:

<class 'str'>

A white space written inside quotation marks is also considered to be a string type. See the example below:

# creating string x = " " # printing type print(type(x))<code>

Output:

<class 'str'>

Python compare strings ignore cases

Now let us compare two strings in python. In this particular section we will develop a python program to compare between strings to ignore cases or case-sensitive. Python is a case sensitive programming language. Here uppercase letter is not equal to a lowercase letter. That means when we compare two strings in python they should be exactly the same, even the case otherwise they will be considered to be different. See the example below:

str1 = "Hello" str2 = "hello" print(str1==str2)

Output:

False

However, we can develop a case-insensitive program which means that it will ignore the uppercase or lowercase differences.

Python compare strings ignore case using lower function

In Python there is a built-in function known as lower() function which converts all the uppercase characters in a string to a lowercase character. See the example of how we can ignore the case sensitivity using a lower function.

str1 = "Hello" str2 = "hello" print(str1.lower()==str2.lower())

Output:

True

 

ALSO READ: Python writelines() Method [Practical Examples]

Notice that this time we get True because lower() function converts all the characters into lower case and then compares the two strings.

See one more example here which compares.

# strings str1 = "My name Is Bashir alam" str2 = "my name is bashir alam" # if statement to compare two strings if str1.lower() == str2.lower():    print("they are same") else:    print("they are not same")

Output:

they are same

Python compare strings ignore case using upper method

We also have a built-in function known as upper() which converts all the lower case characters to upper case. It works in a similar way we lower works. See the example below:

# strings str1 = "Hello" str2 = "hello" # check using upper function print(str1.upper()==str2.upper())

Output:

True

In a similar way, we can use them in if-else statements as well. See one more example below:

# strings str1 = "My name Is Bashir alam" str2 = "my name is bashir alam" # if statement to compare two strings if str1.upper() == str2.upper():    print("they are same") else:    print("they are not same")

Output:

they are same

Python compare strings ignore case using casefold function

The casefold function is another python built-in function that works similar to lower and upper methods. But unlike lower() and upper() which performs a strict string comparison by removing all the case distinctions, the casefold method is used for caseless matching. For example the german letter ß is equal to ss in english. If we apply an upper or lower method to compare this character with ss, it will return False, but the casefold will convert ß to ss and return True. See the following example.

# strings str1 = "ß" str2 = "ss" # check using upper function print(str1.casefold()==str2.casefold())

Output:

True

 

ALSO READ: Python Ternary Operator Explained [Easy Examples]

Python compare two strings characters by characters

Another way to compare two strings is to compare character by character. As we know strings are iterable so we can iterate over them and compare each character. There can be many ways to iterate but here we will see the simplest one. See the example below which used for loop to iterate over string and compares both:

# function to compare two strings def stringCompare(str1, str2):    # for loop to iterate    for i in range(len(str1)):        # check each characters        if str1[i]!=str2[i]:            # return false is one of the character is not equal            return False        else:            pass    # if all characters are equal, it returns true    return True print(stringCompare("my name is bashir alam", "my name is bashir alam"))

Output:

True

In the above example, the function takes two strings as an input and uses a for loop and then compares each character of strings by specifying their index number.

In a similar way we can compare character by character by ignoring the case difference. See the example which ignores the case difference.

# function to compare two strings def stringCompare(str1, str2):    # for loop to iterate    for i in range(len(str1)):        # check each characters        if str1[i].lower()!=str2[i].lower():            # return false is one of the character is not equal            return False        else:            pass    # if all characters are equal, it returns true    return True print(stringCompare("My NamE is BAshir ALam", "my name is bashir alam"))

Output:

True

We still get True because the lower() method converts all the upper case into lowercase and then compares the characters.

ALSO READ: 5 easy ways to concatenate strings in Python with examples

Python compare strings and return the difference

There might be a situation where we wanted to find out the difference between two strings, or we wanted to compare two strings but it returns False so we wanted to figured out the difference between those two. In python there can be many ways to achieve this but here we will take the most simplest one. See the following code.

# function to print the difference def compareString(str1, str2):    # creating empty variables    result1 = ''    result2 = ''    #handle the case where one string is longer than the other    maxlen=len(str2) if len(str1)<len(str2) else len(str1)    #loop through the characters    for i in range(maxlen):        #use a slice rather than index in case one string longer than other        letter1=str1[i:i+1]        letter2=str2[i:i+1]        #create string with differences        if letter1 != letter2:            # adding letters to result            result1+=letter1            result2+=letter2    # printing the difference    print ("Letters different in str1: ",result1)    print ("Letters different in str2: ",result2) # calling the function compareString("Bashir", 'bashir')

Output:

Letters different in str1:  B Letters different in str2:  b

Notice that in the given string only the B was different and this program successfully returns the different value.  Here is another way of applying the same logic. See the code below which returns the difference values.

# function to print the difference def compareString(str1, str2):    # creating empty strings    result1 = ""    result2 = ""    # looping str1 and str2    for a, b in zip(str1, str2):        # check if equal or not        if a != b:            result1+=a            result2+=b    return result1 +"  and  " +result2 # calling the function print(compareString("Bashir", 'bAshir'))

Output:

Ba  and  bA

As we mentioned earlier, there can be many more methods to achieve the same result. You can choose any of them depending on your coding skills.

ALSO READ: Hackerrank Solution: Map and lambda function in Python

Python compare strings ignore whitespaces

Now let's see the following simple example where we will try to compare two strings, one with whitespace before it and one without any white space.  See the example below:

# strings str1 = "B" str2 = " B" # compare two strings print(str1==str2)

Output:

False

We get False because whitespaces are also considered to be characters in python. Suppose we don't want our program to consider whitespaces, in python we can achieve this by a simple piece of code. Python provides us a built-in function known as strip() which helps us to achieve the above-mentioned logic and it ignores the white spaces.

# strings str1 = "B" str2 = " B" # compare two strings using strip function print(str2.strip() == str1.strip())

Output:

True

This time it returns True because the strip function ignores the whitespaces and then we compare the strings.

See one more example which contains a lot of white spaces.

# strings str1 = "This is Bashir Alam" str2 = "        This is Bashir Alam" # compare two strings using strip function if str1.strip()==str2.strip():    print("both are same") else:    print("Both are different")

Output:

both are same

Python compare strings not equal operator

So far we have compared the strings using python equality sign ( == ). However, python also supports us to compare strings based on not equality sign (!=). This will return true if both of the strings are not equal otherwise it will return False. See the example below:

# strings str1 = "Bashir" str2 = "Alam" # compare strings print(str1!=str2)

Output:

True

It returns True because both of the strings are not equal and our assumption is True.

In Python is not is also an alternative to compare two strings. It will also return True if both the strings are not the same.  See the example which uses is not instead of != operator

# strings str1 = "Bashir" str2 = "Alam" # compare strings print(str1 is not str2)

Output:

True

 

ALSO READ: Python multi line strings [5 Methods Explained]

Python compare strings with wildcard

The asterisk (*) is used to specify any number of characters. It is typically used at the end of a root word. This is great when you want to search for variable endings of a root word. For example, searching for clean* would tell the database to look at all the possible words ending to root clean. Result might be cleaning, cleaner or cleaned depending on our list of words.

While question mark (?) is used to represent a single character anywhere in the word. It is most useful when there are variable spellings for a word, and we want to search for all variants at once. For example, searching for l?st might return list or lost depending on the words available.

Now let us take an example of both asterisk and question mark to search for different words. See the python code below:

# Regular expression library import re # list of different words WordList = ["list", "clean", "lost","cleaner"           , "worker", "working"] # for loop to iterate our list of words for word in WordList:        # The . symbol is used in place of ? symbol        if re.search('l.st', word) :            # printing the word                print (word)

Output:

list lost

Now let us take the example of asterisk and see what we get for clean*. See the example below:

# Regular expression library import re # list of different words WordList = ["list", "clean", "lost","cleaner"            , "worker", "working"] # for loop to iterate our list of words for word in WordList:        # The .+ symbol is used in place of * symbol        if re.search('clean.+', word) :            # printing the word            print (word)

Output:

cleaner

ALSO READ: 5 easy ways to concatenate strings in Python with examples

Python compare strings in a list

Python provides us various ways to compare two lists. Comparison is the process when the data items of one side are checked against another data item to see whether they are the same or not. Here we will compare the data of two sets and see if they are equal or not.

See the example below which direct equality sign to compare two lists containing strings as a data set.

# lists containing strings list1 = ["a", "b", "c", "d"] list2 = ["a", "b", "c", "d"]  # compares two lists if list1 == list2:    print("The list1 and list2 are equal") else:    print("The list1 and list2 are not equal")

Output:

The list1 and list2 are equal

Suppose we want to compare two sets and we assume that it should return True for duplicates values as well. We can achieve this by using the set() method, which will first convert our list to set and delete any duplicate elements. See the example below:

# lists containing strings list1 = ["a", "b", "c", "d", "d"] list2 = ["a", "b", "c", "d"]  # compares two lists if set(list1) == set(list2):    print("The list1 and list2 are equal") else:    print("The list1 and list2 are not equal")

Output:

The list1 and list2 are equal

Notice that list1 contains duplicates elements but still we get True for comparison because set() method removed the duplicate values.

ALSO READ: Python lambda function - with simple examples

Another method of comparing two sets of strings might be to loop over them and compare each element and if they return True if all are the same. See the python program below which compares the list of strings using for loop.

# function to compare list def compareList(str1, str2):    # for loop to iterate    for i in range(len(str1)):        # check if data is not equal        if str1[i]!=str2[i]:            # return False            return False        else:            pass    # return True    return True # calling the function print(compareList(["a", "b", "c", "d"] ,["a", "b", "c", "d"] ))

Output:

True

Python compare strings lexicographically

We use comparison operators to compare strings lexicographically in Python. We already have used equal and not equal operators to compare strings. Here is a list of comparison operators that are used to compare two strings.

  • Equal to == : check if two strings are equal
  • Not equal to !=: check if two strings are not equal
  • Greater then > : check if a string is greater than other
  • Less than < : check if a string is less than other

As we already have used the first two operators, in this section we will focus on the last two comparison operators. See the following example which compares two strings.

# strings str1 = "B" str2 = "b" # comparing print(str1 > str2) print(str2 > str1)

Output:

False True

Notice that the upper case B is smaller than the lower case b because in the ASCII  set, the numeric value of upper B is 66 while that of lower one is 98. In python all the uppercase alphabets have smaller numeric values ( starting from 65 ) and all the lower case alphabets have larger numeric values ( starting from 97).

ALSO READ: Flask SQLAlchemy explained with examples

See the example below which uses less than operator. This time it will return True for uppercase letters.

# strings str1 = "B" str2 = "b" # comparing print(str1 < str2) print(str2 < str1)

Output:

True False

Now let us compare two lists containing strings dataset. See the example below:

# strings str1 = ["bashir", "alam"] str2 = ["bashir", "Alam"] # comparing print(str1 < str2) print(str2 < str1)

Output:

False True

Summary

Strings are sequences of characters that can include numbers, letters, symbols and whitespaces. Everything in python that is written inside single, double or triple quotation marks will be considered as string.  Python strings are an important data type because they allow us to interact with text-based data in our programs. In this tutorial we learned about string comparison. We covered various ways to compare strings in python using different comparison operators and examples.

Further Reading Section

python compare strings
Python string
comparison operators