Python is a versatile language having many built in methods and libraries. Strings and substrings are an important part of every programming language; python provides different methods to deal with strings and substrings, we check if a python string has a substring for a variety of reasons, but conditional statements are the most typical application. To find substrings in a string, python language provides many predefined methods.

In this Article we will discuss some of methods mentioned below to search for substrings in strings in python on Ubuntu (Linux OS), you can follow any of the method according to your requirement:

  • By using “in” operator
  • By using find() method
  • by using index() method

Requirement

Any Python version needs to be installed on your Ubuntu System( Python3 is pre-installed on latest Ubuntu versions)

How to Create Python File

To code in python on Linux System you have to create a Python file with “.py” extension. Create “python_file.py” by below mentioned command:

Important Note

  • # is used in code screenshots, the statement after # are comments(explanatory statements) not executed during code execution
  • Press “Ctrl s” to save the python file and “Ctrl x” to exit the python file.

How to check for substring by using ‘in’ operator

One of the easiest ways to check if a specified substring exists in String or not in python is with the help of the “in” operator. The “in” operator returns a Boolean value of “true” if the substring exists and “false” if it does not exist.

Below mentioned is the syntax of “in” operator to check for substring:

Replace [substring] with substring you want to find in specific [string]. To find substring “linux” in string “This is linuxhint”, write the below mentioned code in newly created “python_file.py” file above:

print(n Find substring linux in “This is linuxhint” using in operator “)

if “linux” in “This is linuxhint”:

print(n substring found!!”)

else:

print(n substring not found!!”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-263.png" data-lazy- height="676" src="data:image/svg xml,” width=”1055″>

To output result on terminal, execute the code written in “python_file.py” by below mentioned command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-264.png" data-lazy- height="681" src="data:image/svg xml,” width=”1057″>

How to check for substring using String “find ()” method

The String “find()” method can be used to search for substring in a specified string. If substring exists then it returns the starting index value of substring else it returns “-1” if the substring does not exist.

Below mentioned is the syntax if find() method to search for substring:

string.find([substring], [start-index],[end-index])

string: string variable in which string is stored you can give the string value directly here.

substring: replace it with the substring you want to find.

start-index(optional): the starting point/index to search for substring.

end-index(optional): the ending point/index to search for substring.

To find the index of “linux” in “This is linuxhint”, write the below mentioned code in”python_file.py”:

print(n Find substring linux in “This is linuxhint” using find() method”)

string=“This is linuxhint”

index= string.find(“linux”)

if index !=1:

print(n index of linux is: “, index)

else:

print(n substring not found!!”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-265.png" data-lazy- height="679" src="data:image/svg xml,” width=”1061″>

To output result on terminal, execute the code written in “python_file.py” by below mentioned command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-266.png" data-lazy- height="286" src="data:image/svg xml,” width=”1053″>

To check for substring “linux” between “index 1” and “index 7” write the below mentioned code in the “python_file.py”

print(n Find substring linux in “This is linuxhint” using find() method”)

string=“This is linuxhint”

index= string.find(“linux”,1,7)

if index !=1:

print(n index of linux is: “, index)

else:

print(n substring not found!!”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-267.png" data-lazy- height="672" src="data:image/svg xml,” width=”1057″>

To output result on terminal, execute the code written in “python_file.py” by below mentioned command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-268.png" data-lazy- height="257" src="data:image/svg xml,” width=”1055″>

How to check for substring using String “index ()” method

The String index() method returns the starting index of a substring if it exists. It is very similar to the find() method except that it sends an exception if a substring is not found so we have to use the “try except” block for it, it is case sensitive so write the substring carefully.

string.index([substring], [start-index],[end-index])

string: string variable in which string is stored/ you can give the string value directly here.

substring: replace it with the substring you want to find.

start-index(optional): the starting point/index to search for substring.

end-index(optional): the ending point/index to search for substring.

Example1:

To find the index of “linux” in “This is linuxhint”, write the below mentioned code in “python_file.py”:

print(n Find substring linux in “This is linuxhint” using index() method”)

string =“This is linuxhint”

try:

string.index(“linux”)

print(“substring found!!”)

except ValueError

print(“substring not found!!”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-269.png" data-lazy- height="673" src="data:image/svg xml,” width=”1069″>

To output result on terminal, execute the code written in “python_file.py” by below mentioned command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-270.png" data-lazy- height="255" src="data:image/svg xml,” width=”1072″>

Example 2:

To check for substring “linux” between “index 1” and “index 7” write the below mentioned code in the “python_file.py” file:

print(n Find substring linux in “This is linuxhint” using index() method”)

string =“This is linuxhint”

try:

string.index(“linux”,1,7)

print(“substring found!!”)

except ValueError

print(“substring not found!!”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-271.png" data-lazy- height="677" src="data:image/svg xml,” width=”1069″>

To get output on terminal, execute the code written in “python_file.py” by below mentioned command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-272.png" data-lazy- height="676" src="data:image/svg xml,” width=”1072″>

In the above output , the keyword “linux” is not found as it comes between index7 and index9 so to get “linux” keyword you must include these indexes.

To check for substring “linux” between “index 1” and “index 15” write the below mentioned code in the “python_file.py” file:

print(n Find substring linux in “This is linuxhint” using index() method”)

string =“This is linuxhint”

try:

string.index(“linux”,1,15)

print(“substring found!!”)

except ValueError

print(“substring not found!!”)

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-273.png" data-lazy- height="677" src="data:image/svg xml,” width=”1111″>

To get output on terminal, execute the code written in “python_file.py” by below mentioned command:

<img data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2021/10/echo/word-image-274.png" data-lazy- height="279" src="data:image/svg xml,” width=”1110″>

Now the string is found as “linux” complete substring lies between the specified index.

Conclusion:

While programming we deal with strings and substrings on a daily basis. They are an integral part of programming. In this article we have discussed some methods to find substring from string in Python on Ubuntu (Linux System). The methods we discussed in this article include how to find substring using “in” operator, find() method, index method(). After reading this article, you will be able to find substring in string from the methods mentioned above and use them accordingly in your program.

About the author

<img alt="" data-lazy-src="https://secure.gravatar.com/avatar/7c99efcc024949e17b176845c757687e?s=112&r=g" data-lazy- height="112" src="data:image/svg xml,” width=”112″>

Alishba Iftikhar

I am currently an undergraduate student in my 1st year. I am an internee author with Linuxhint and loved learning the art of technical content writing from senior authors. I am looking forward to opting my career as a full time Linux writer after I graduate.