{"id":4661,"date":"2021-04-29T12:24:17","date_gmt":"2021-04-29T06:54:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=4661"},"modified":"2021-11-22T18:42:59","modified_gmt":"2021-11-22T13:12:59","slug":"python-check-if-a-string-contains-a-sub-string-case-insensitive","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-check-if-a-string-contains-a-sub-string-case-insensitive\/","title":{"rendered":"Python : Check if a String contains a Sub String | Case Insensitive"},"content":{"rendered":"
Strings are one of the most commonly used types in Python. We can easily make them by enclosing characters in quotes. Python considers single quotes to be the same as double quotes. String creation is as easy as assigning a value to a variable.<\/p>\n
Let’s look at different ways to solve the general problem of determining whether a specific piece of string is present in a larger string. This is a very common type of problem that every programmer encounters at least once in his or her career. This article discusses various methods for resolving it.<\/p>\n
Given a string and substring ,the task is to check if the substring is present in given string.<\/p>\n
Examples:<\/strong><\/p>\n 1)Case sensitive<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Case insensitive<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n There are several ways to check whether a string contains given substring some of them are:<\/p>\n The in operator is the most generic and fastest way to check for a substring in Python. The power of the in operator in Python is well known and is used in many operations throughout the language.<\/p>\n 1)Case sensitive<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Case Insensitive<\/strong><\/p>\n We can check them by converting both given string and substring to lower case or uppercase.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Similarly, we can use the \u201cnot in\u201d operator to test the opposite scenario, that is, to see if a string or character does not exist in another string.<\/p>\n 1)Case sensitive<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Case insensitive<\/strong><\/p>\n We can check them by converting both given string and substring to lower case or uppercase.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n The str.find() method is generally used to get the lowest index at which the string occurs, but it also returns -1 if the string is not present; thus, if any value returns greater than zero, the string is present; otherwise, it is not present.<\/p>\n 1)Case sensitive<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Case insensitive<\/strong><\/p>\n We can check them by converting both given string and substring to lower case or uppercase.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n This method can be used to perform the same task as str.find(), but unlike str.find(), it does not return a value, but rather a ValueError if string is not present, so catching the exception is the only way to check for string in substring.<\/p>\n 1)Case sensitive<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n 2)Case insensitive<\/strong><\/p>\n We can check them by converting both given string and substring to lower case or uppercase.<\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Related Programs<\/strong>:<\/p>\n Strings are one of the most commonly used types in Python. We can easily make them by enclosing characters in quotes. Python considers single quotes to be the same as double quotes. String creation is as easy as assigning a value to a variable. Let’s look at different ways to solve the general problem of …<\/p>\nstring = 'This is BTechGeeks online platform'\u00a0 substring = 'online'<\/pre>\n
Yes<\/pre>\n
string = 'This is BTechGeeks Online platform'\u00a0 substring = 'online'<\/pre>\n
Yes<\/pre>\n
Determine whether a String contains a Sub String<\/h2>\n
\n
Method #1:Using in operator<\/h3>\n
# given string\r\nstring = 'This is BTechGeeks online platform'\r\n# given substring\r\nsubstring = 'online'\r\n# checking if the substring is present in string\r\nif substring in string:\r\n print('Yes')\r\nelse:\r\n print('No')\r\n<\/pre>\n
Yes<\/pre>\n
# given string\r\nstring = 'This is BTechGeeks Online platform'\r\n# given substring\r\nsubstring = 'online'\r\n# checking if the substring is present in string by converting to lowercase\r\nif substring.lower() in string.lower():\r\n print('Yes')\r\nelse:\r\n print('No')\r\n<\/pre>\n
Yes<\/pre>\n
Method #2: Using not in operator<\/h3>\n
# given string\r\nstring = 'This is BTechGeeks online platform'\r\n# given substring\r\nsubstring = 'online'\r\n# checking if the substring is present in string\r\nif substring not in string:\r\n print('No')\r\nelse:\r\n print('Yes')\r\n<\/pre>\n
Yes<\/pre>\n
# given string\r\nstring = 'This is BTechGeeks online platform'\r\n# given substring\r\nsubstring = 'online'\r\n# checking if the substring is present in string by converting to lowercase\r\nif substring.lower() not in string.lower():\r\n print('No')\r\nelse:\r\n print('Yes')\r\n<\/pre>\n
Yes<\/pre>\n
Method #3:Using str.find()<\/h3>\n
# given string\r\nstring = 'This is BTechGeeks online platform'\r\n# given substring\r\nsubstring = 'online'\r\n# using find operator\r\nresult = string.find(substring)\r\n# if result is greater than 0 then print yes\r\nif result > 0:\r\n print('Yes')\r\nelse:\r\n print('No')\r\n<\/pre>\n
Yes<\/pre>\n
# given string to lower\r\nstring = 'This is BTechGeeks Online platform'.lower()\r\n# given substring to lower\r\nsubstring = 'online'.lower()\r\n# using find operator\r\nresult = string.find(substring)\r\n# if result is greater than 0 then print yes\r\nif result > 0:\r\n print('Yes')\r\nelse:\r\n print('No')\r\n<\/pre>\n
Yes<\/pre>\n
Method #4:Using index() function<\/h3>\n
# given string\r\nstring = 'This is BTechGeeks Online platform'\r\n# given substring\r\nsubstring = 'online'\r\n# using try and except\r\n# using index function\r\ntry:\r\n result = string.find(substring)\r\n print('Yes')\r\nexcept:\r\n print('No')\r\n<\/pre>\n
Yes<\/pre>\n
# given string to lower\r\nstring = 'This is BTechGeeks Online platform'.lower()\r\n# given substring to lower\r\nsubstring = 'online'.lower()\r\n# using try and except\r\n# using index function\r\ntry:\r\n result = string.find(substring)\r\n print('Yes')\r\nexcept:\r\n print('No')\r\n<\/pre>\n
Yes<\/pre>\n
\n