{"id":4789,"date":"2021-05-01T15:02:10","date_gmt":"2021-05-01T09:32:10","guid":{"rendered":"https:\/\/python-programs.com\/?p=4789"},"modified":"2021-11-22T18:42:59","modified_gmt":"2021-11-22T13:12:59","slug":"python-how-to-compare-strings-ignore-case-regex-is-vs-operator","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/","title":{"rendered":"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator"},"content":{"rendered":"

How to Compare Strings ? | Ignore case | regex | is vs == operator in Python ?<\/h2>\n

In this article we will discuss various ways to compare strings in python.<\/p>\n

Python provides various operators for comparing strings i.e less than(<), greater than(>), less than or equal to(<=), greater than or equal to(>=), not equal(!=), etc and whenever they are used they return Boolean values i.e True or False.<\/p>\n

Compare strings using == operator to check if they are equal using python :<\/h3>\n

Let\u2019s see it with an example:<\/p>\n

#program :\r\n\r\nfirststr = 'First'\r\nsecondstr = 'second'\r\n\r\nif firststr == secondstr:\r\n\u00a0\u00a0\u00a0 print('Strings are same')\r\nelse:\r\n\u00a0\u00a0\u00a0 print('Strings are not same')<\/pre>\n
Output:\r\nStrings are not same<\/pre>\n

As the content of both the string are not same so it returned False.<\/p>\n

#Program :\r\n\r\nfirststr = 'done'\r\nsecondstr = 'done'\r\n\r\nif firststr == secondstr:\r\n\u00a0\u00a0\u00a0 print('Strings are same')\r\nelse:\r\n\u00a0\u00a0\u00a0 print('Strings are not same')<\/pre>\n
Output:\r\nStrings are same<\/pre>\n

Here, as the content of both the string are not same so it returned True.<\/p>\n

Compare strings by ignoring case using python :<\/h3>\n

Let\u2019s see it with an example:<\/p>\n

#program :\r\n\r\nfirststr = 'PROGRAM'\r\nsecondstr = 'program'\r\n\r\nif firststr.lower() == secondstr.lower():\r\n\u00a0\u00a0\u00a0 print('Strings are same')\r\nelse:\r\n\u00a0\u00a0\u00a0 print('Strings are not same')<\/pre>\n
Output :\r\nStrings are same<\/pre>\n

As we can see that both the strings are same but are in different case. Now let\u2019s try with another operator.<\/p>\n

Check if string are not equal using != operator using python :<\/h3>\n

Let\u2019s see it with an example:<\/p>\n

#Program :\r\n\r\nfirststr = 'python'\r\nsecondstr = 'program'\r\n\r\nif firststr != secondstr:\u00a0\u00a0\u00a0\r\n  print('Strings are not same')\r\nelse:\u00a0\u00a0\u00a0\r\n  print('Strings are same')<\/pre>\n
Output: \r\nStrings are not same<\/pre>\n

Here, the two strings are not same so it returned True.<\/p>\n

Let\u2019s try some other operators.<\/p>\n

Check if one string is less than or greater than the other string :<\/h3>\n

Let\u2019s see it with an example:<\/p>\n

#Program :\r\n\r\nif 45 > 29:\r\n\u00a0\u00a0\u00a0 print('\"45\" is greater than \"29\"')\r\nif \"abc\" > \"abb\":\r\n\u00a0\u00a0\u00a0 print('\"abc\" is greater than \"abb\"')\r\nif \"ABC\" < \"abc\":\r\n\u00a0\u00a0\u00a0 print('\"ABC\" is less than \"abc\"')\r\nif 32 >= 32:\r\n\u00a0\u00a0\u00a0 print('Both are equal')\r\nif 62 <= 65:\r\n\u00a0\u00a0\u00a0 print('\"62\" is less than 65')<\/pre>\n
Output :\r\n\"45\" is greater than \"29\"\r\n\"abc\" is greater than \"abb\"\r\n\"ABC\" is less than \"abc\"\r\nBoth are equal\r\n\"62\" is less than 65<\/pre>\n

Comparing strings : is vs == operator :<\/h3>\n

Python has the two comparison operator ==<\/code> and is<\/code>. At first sight they seem to be the same, but actually they are not.<\/p>\n

==<\/code> compares two variable based on their actual value but is operator compares two variables based on the object id and returns True if the two variables refer to the same object and returns False if two variables refer to the different object.<\/p>\n

Sometimes is<\/code> operator is also used to compare strings to check if they are equal or not.<\/p>\n

Compare contents using is operator :<\/h4>\n

Example-1<\/h4>\n
#Program :\r\n\r\na = 5\r\nb = 5\r\nif a is b:\r\n print(\"they are same\")\r\nprint('id of \"a\"',id(a))\r\nprint('id of \"b\"',id(b))<\/pre>\n
Output :\r\nthey are same\r\nid of \"a\" 140710783821728\r\nid of \"b\" 140710783821728<\/pre>\n

Example 2:<\/h4>\n
#Program :\r\n\r\na = \"python\"\r\nb = \"program\"\r\nif a is b:\r\n  print(\"they are same\")\r\nelse:\r\n  print(\"they are not same\")\r\n\r\nprint('id of \"a\"',id(a))\r\nprint('id of \"b\"',id(b))<\/pre>\n
Output:\r\nthey are not same\r\nid of \"a\" 2104787270768\r\nid of \"b\" 2104783497712<\/pre>\n

Compare contents using == operator :<\/h4>\n

The ==<\/code> operator compares the value or equality of two objects.<\/p>\n

#Program :\r\n\r\na = 5\r\nb = 5\r\n\r\nif a == b:\r\n  print('Both are same')<\/pre>\n
Output:\r\n\r\nBoth are same<\/pre>\n

Compare strings using regex in python :<\/h3>\n

A regular expression(re)\u00a0 or regex is a special text string used for describing a search pattern. We can use that to compare strings.<\/p>\n

#program :\r\n\r\n\r\nimport re\r\nrepattern = re.compile(\"88.*\")\r\n#list of numbers\r\nList = [\"88.3\",\"88.8\",\"87.1\",\"88.0\",\"28.2\"]\r\n\r\n#check if strings in list matches the regex pattern\r\nfor n in List:\r\n match = repattern.fullmatch(n)\r\n if match:\r\n  print('string',n ,'matched')\r\n else:\r\n\u00a0 print('string',n ,'do not matched')<\/pre>\n
Output :\r\nstring 88.3 matched\r\nstring 88.8 matched\r\nstring 87.1 do not matched\r\nstring 88.0 matched\r\nstring 28.2 do not matched<\/pre>\n","protected":false},"excerpt":{"rendered":"

How to Compare Strings ? | Ignore case | regex | is vs == operator in Python ? In this article we will discuss various ways to compare strings in python. Python provides various operators for comparing strings i.e less than(<), greater than(>), less than or equal to(<=), greater than or equal to(>=), not equal(!=), …<\/p>\n

Python : How to Compare Strings ? | Ignore case | regex | is vs == operator<\/span> Read More »<\/a><\/p>\n","protected":false},"author":9,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"spay_email":"","jetpack_publicize_message":"","jetpack_is_tweetstorm":false,"jetpack_publicize_feature_enabled":true},"categories":[5],"tags":[],"yoast_head":"\nPython : How to Compare Strings ? | Ignore case | regex | is vs == operator - Python Programs<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator - Python Programs\" \/>\n<meta property=\"og:description\" content=\"How to Compare Strings ? | Ignore case | regex | is vs == operator in Python ? In this article we will discuss various ways to compare strings in python. Python provides various operators for comparing strings i.e less than(<), greater than(>), less than or equal to(<=), greater than or equal to(>=), not equal(!=), … Python : How to Compare Strings ? | Ignore case | regex | is vs == operator Read More »\" \/>\n<meta property=\"og:url\" content=\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/\" \/>\n<meta property=\"og:site_name\" content=\"Python Programs\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/btechgeeks\" \/>\n<meta property=\"article:published_time\" content=\"2021-05-01T09:32:10+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2021-11-22T13:12:59+00:00\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:site\" content=\"@btech_geeks\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Satyabrata Jena\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Organization\",\"@id\":\"https:\/\/python-programs.com\/#organization\",\"name\":\"BTech Geeks\",\"url\":\"https:\/\/python-programs.com\/\",\"sameAs\":[\"https:\/\/www.instagram.com\/btechgeeks\/\",\"https:\/\/www.linkedin.com\/in\/btechgeeks\",\"https:\/\/in.pinterest.com\/btechgeek\/\",\"https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg\",\"https:\/\/www.facebook.com\/btechgeeks\",\"https:\/\/twitter.com\/btech_geeks\"],\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"contentUrl\":\"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png\",\"width\":350,\"height\":70,\"caption\":\"BTech Geeks\"},\"image\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/python-programs.com\/#website\",\"url\":\"https:\/\/python-programs.com\/\",\"name\":\"Python Programs\",\"description\":\"Python Programs with Examples, How To Guides on Python\",\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/python-programs.com\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#webpage\",\"url\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/\",\"name\":\"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator - Python Programs\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/#website\"},\"datePublished\":\"2021-05-01T09:32:10+00:00\",\"dateModified\":\"2021-11-22T13:12:59+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/python-programs.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator\"}]},{\"@type\":\"Article\",\"@id\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#webpage\"},\"author\":{\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\"},\"headline\":\"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator\",\"datePublished\":\"2021-05-01T09:32:10+00:00\",\"dateModified\":\"2021-11-22T13:12:59+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#webpage\"},\"wordCount\":353,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/python-programs.com\/#organization\"},\"articleSection\":[\"Python\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#respond\"]}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd\",\"name\":\"Satyabrata Jena\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/python-programs.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g\",\"caption\":\"Satyabrata Jena\"},\"url\":\"https:\/\/python-programs.com\/author\/satyabrata\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator - Python Programs","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/","og_locale":"en_US","og_type":"article","og_title":"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator - Python Programs","og_description":"How to Compare Strings ? | Ignore case | regex | is vs == operator in Python ? In this article we will discuss various ways to compare strings in python. Python provides various operators for comparing strings i.e less than(<), greater than(>), less than or equal to(<=), greater than or equal to(>=), not equal(!=), … Python : How to Compare Strings ? | Ignore case | regex | is vs == operator Read More »","og_url":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/","og_site_name":"Python Programs","article_publisher":"https:\/\/www.facebook.com\/btechgeeks","article_published_time":"2021-05-01T09:32:10+00:00","article_modified_time":"2021-11-22T13:12:59+00:00","twitter_card":"summary_large_image","twitter_creator":"@btech_geeks","twitter_site":"@btech_geeks","twitter_misc":{"Written by":"Satyabrata Jena","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Organization","@id":"https:\/\/python-programs.com\/#organization","name":"BTech Geeks","url":"https:\/\/python-programs.com\/","sameAs":["https:\/\/www.instagram.com\/btechgeeks\/","https:\/\/www.linkedin.com\/in\/btechgeeks","https:\/\/in.pinterest.com\/btechgeek\/","https:\/\/www.youtube.com\/channel\/UC9MlCqdJ3lKqz2p5114SDIg","https:\/\/www.facebook.com\/btechgeeks","https:\/\/twitter.com\/btech_geeks"],"logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/","url":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","contentUrl":"https:\/\/python-programs.com\/wp-content\/uploads\/2020\/11\/BTechGeeks.png","width":350,"height":70,"caption":"BTech Geeks"},"image":{"@id":"https:\/\/python-programs.com\/#\/schema\/logo\/image\/"}},{"@type":"WebSite","@id":"https:\/\/python-programs.com\/#website","url":"https:\/\/python-programs.com\/","name":"Python Programs","description":"Python Programs with Examples, How To Guides on Python","publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/python-programs.com\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#webpage","url":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/","name":"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator - Python Programs","isPartOf":{"@id":"https:\/\/python-programs.com\/#website"},"datePublished":"2021-05-01T09:32:10+00:00","dateModified":"2021-11-22T13:12:59+00:00","breadcrumb":{"@id":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/python-programs.com\/"},{"@type":"ListItem","position":2,"name":"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator"}]},{"@type":"Article","@id":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#article","isPartOf":{"@id":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#webpage"},"author":{"@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd"},"headline":"Python : How to Compare Strings ? | Ignore case | regex | is vs == operator","datePublished":"2021-05-01T09:32:10+00:00","dateModified":"2021-11-22T13:12:59+00:00","mainEntityOfPage":{"@id":"https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#webpage"},"wordCount":353,"commentCount":0,"publisher":{"@id":"https:\/\/python-programs.com\/#organization"},"articleSection":["Python"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/python-programs.com\/python-how-to-compare-strings-ignore-case-regex-is-vs-operator\/#respond"]}]},{"@type":"Person","@id":"https:\/\/python-programs.com\/#\/schema\/person\/0f6d731bda7051ce3586f71299f391cd","name":"Satyabrata Jena","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/python-programs.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c1bf8033ec357d085f41815bee1625cc?s=96&d=mm&r=g","caption":"Satyabrata Jena"},"url":"https:\/\/python-programs.com\/author\/satyabrata\/"}]}},"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4789"}],"collection":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/comments?post=4789"}],"version-history":[{"count":1,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4789\/revisions"}],"predecessor-version":[{"id":4790,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/posts\/4789\/revisions\/4790"}],"wp:attachment":[{"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/media?parent=4789"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/categories?post=4789"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/python-programs.com\/wp-json\/wp\/v2\/tags?post=4789"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}