Python String Substring

  • By Mayuresh Marale
  • August 21, 2023
  • Python
Python String Substring

Python String Substring

A substring is for a part of a Python string object, that can be searched, found, and processed using built-in methods. Checking if a string contains a substring comes in handy when you have received user input and want your program to behave a certain way – or even when you want to replace one word with another. Python String Substring offers many ways to confirm whether a substring is present in a string, and in this quick guide, you will see a few of them in action. Join Python Course in Pune now and embark on an exciting journey into the world of Python programming

 

Here is what we will discuss:

  1. How to check if string contains substring using the in operator
  2. How to check if string contains substring using the .index() method
  3. How to check if string contains substring using the .find() method
  4. How to count instances of substring in string using the .count() method
  5. How to replace old substring with new substring in string using the .replace() method

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

How to Check if a String Contains a Substring Using the in Operator in Python

Using the in operator is one of the clearest, most readable, and most Pythonic ways to confirm whether a substring is present in a string.

Therefore, the in-operator is the preferred way of checking that a string contains a substring.

 

The general syntax for using the in operator: substring in a string

 

The in operator returns True if the substring is present in the string and False if the substring is not present.

string = ‘hello world’

substring = ‘world’

print(substring in string)

# output : True

Since the substring is present here, in returns True.

 

It is important to note that the in operator only checks if the substring is present and exists in the string. It doesn’t check the position of the substring, nor does it give any information on how many times the substring appears in the string.

 

How to Perform a Case-Insensitive Search When Using the in Operator in Python

You can convert the whole user input into lowercase by using the .lower() method:

string = ‘Hello World’

substring = ‘World’

print(substring.lower() in string.lower())

# output : True

Since you have converted the substring and string to lowercase using .lower() method, in returns True if string contains substring.

 

How to Check if a String Contains a Substring Using the .index() Method in Python

You can use the .index() Python method to find the starting index of the first character of the first occurrence of a substring inside a string:

string = ‘Hello World’

substring = ‘World’

print(string.index(substring))

# output : 6

We called the .index() method on the string and passed the substring as the argument to find where the first free substring occurrence appears. (The string contains one instance of the substring).

The .index() method comes in handy when you want to know the location of the substring you are searching for and where the substring occurs and starts in the string.

The in operator lets you know whether the substring exists in the string, whereas the .index() method also tells you where it exists. Supercharge your programming skills with Python classes in Pune.

 

That said, .index() is not ideal when Python can’t find the substring in the string because it raises a ValueError error.

 

string = ‘Hello World’

substring = ‘Universe’

print(string.index(substring))

# output

# ValueError: substring not found

If you want to avoid that error from being raised when searching for a string, and you don’t want to use the in operator, you can use the Python find() method instead.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

How to Check if a String Contains a Substring Using the .find() Method in Python

The .find() method works similarly to the .index() method – it checks to see if a string contains a substring, and if the substring is present, it returns its starting index.

string = ‘Hello World’

substring = ‘World’

print(string.find(substring))

# output : 6

The difference between the .find() method and the .index() method is that with .find(), you don’t have to worry when it comes it handling exceptions, in comparison to .index().

string = ‘Hello World’

substring = ‘Universe’

print(string.find(substring))

# output : -1

On the other hand, when you are using the .find() method and the string doesn’t contain the substring you are searching for, .find() returns -1 without raising an exception.

 

For Free, Demo classes Call: 02071171500

Registration Link: Click Here!

 

How to count instances of substring in a string using the .count() method

The .count() method counts all instances of substring in string.

string = ‘Hello World’

substring = ‘World’

print(string.count(substring))

# output : 1

.count() method returns 0 if the substring is not found.

string = ‘Hello World’

substring = ‘Universe’

print(string.find(substring))

# output : 0

 

Note: Do watch our video on Demand of Python Programming

How to replace an old substring with a new substring in a string using the .replace() method

 

The .replace () method returns a copy of the string after the replacement of all instances of the old substring with the new substring if the present string.

string = ‘Hello World’

old = ‘World’

new = ‘Universe’

print(string.replace(old, new))

# output : ‘Hello Universe’

.replace() method returns the original string, if the substring is not found.

string = ‘Hello World’

old = ‘Nation’

new = ‘Planet’

print(string.find(old, new))

# output : ‘Hello World’

 

Author:-

Mayuresh Marale

Call the Trainer and Book your free demo Class For Python

Call now!!! | SevenMentor Pvt Ltd.

© Copyright 2021 | SevenMentor Pvt Ltd.

Submit Comment

Your email address will not be published. Required fields are marked *

*
*