Now print the contents and object ID of both the objects i.e. ![]() Now let’s change the second variable secStr i.e. if firstStr is secStr:Īs both variables were referring to same object, so is operator returned True. Now we will compare these two string variable using is operator, then it will check if both the variables points to same object internally i.e. Print("Object ID of Second object :", id(secStr)) Print("Object ID of First object :", id(firstStr)) We can confirm by print their object ID’s i.e. Now both the variables firstStr & secStr points to same object. if two given variables points to same object or not. It’s used to checks for the equality of two objects i.e. But it will not always work because there is a fundamental difference in functionality of is and = operator in python. Sometimes is operator is also used to compare strings to check if they are equal or not. Similarly we can use = operator to compare strings in lexical order. Let’s see actual example, if "abcd" > "abcc": Operator, = compares the strings in alphabetical order. Check if one string is greater than or less than other string. if firstStr != secStr:Īs contents of both the strings were different, so operator != returned True. Now let’s check if both strings contains different text i.e. To confirm if the contents of two strings are not same we can use != operator too. Check if strings are not equal using != operator using Python It matched the strings in case in sensitive manner. ![]() So to match these strings by ignoring case we need to convert both strings to lower case and then match using operator = i.e. firstStr = "SAMple"Īs both the strings has similar characters but in different case. Now let’s see how to compare strings in case insensitive manner, Compare strings by ignoring case using Python
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |