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 Show
Introduction to Python compare stringsIn 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. Example of Python stringsAs 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 casesNow 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: FalseHowever, 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 functionIn 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: TrueNotice 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. Output: they are samePython compare strings ignore case using upper methodWe 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: TrueIn 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 samePython compare strings ignore case using casefold functionThe 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: TruePython compare two strings characters by charactersAnother 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: TrueIn 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: TrueWe still get True because the lower() method converts all the upper case into lowercase and then compares the characters. Python compare strings and return the differenceThere 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. Output: Letters different in str1: B Letters different in str2: bNotice 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 bAAs 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. Python compare strings ignore whitespacesNow 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: FalseWe 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: TrueThis 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 samePython compare strings not equal operatorSo 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: TrueIt 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: TruePython compare strings with wildcardThe 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 lostNow 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: cleanerPython compare strings in a listPython 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 equalSuppose 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 equalNotice that list1 contains duplicates elements but still we get True for comparison because set() method removed the duplicate values. 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: TruePython compare strings lexicographicallyWe 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.
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 TrueNotice 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). 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 FalseNow 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 TrueSummaryStrings 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 Sectionpython compare strings |