Python Substring Method

Python Substring

In Python, a substring is a sequence of characters that occur within a larger string. A substring can be selected from a string by specifying the starting and ending index of the characters that you want to include in the substring. The characters in a string are indexed, with the first character having an index of 0, and the last character having an index of len(string) – 1.

You can extract a substring from a string using the slice notation, which uses the square brackets [] and a colon : to specify the starting and ending index of the substring. For example:

Python
string = "Hello World!"
substring = string[6:12]
print(substring)  # "World"

In this example, the slice notation ‘string[6:12]‘ selects the characters with indices 6 through 11, which is the substring “World”.

You can also use negative indexing to extract a substring, where -1 is the index of the last character of the string.

Python
string = "Hello World!"
substring = string[-6:-1]
print(substring)  # "World"

In this example, the slice notation ‘string[-6:-1]‘ selects the characters with indices -6 through -2, which is the substring “World”.

Python string contains ‘substring method’

Python has a string method called “in” that can be used to check if a substring is contained within a larger string. The “in” operator returns a Boolean value (True or False) depending on whether the substring is found in the larger string or not.

You can also use the ‘str.find()‘ method that returns the index of the first occurrence of the substring if found otherwise return -1.

For example:

Python
string = "Hello World!"
substring = "World"
result = substring in string
print(result)  # True

string = "Hello World!"
substring = "world"
result = substring in string
print(result)  # False

string = "Hello World!"
substring = "world"
result = string.find(substring)
print(result)  # -1

Also python provides ‘str.index()‘ method that behaves similar to ‘str.find()‘ but raises an exception if the substring is not found.

Extracting a portion of a string

The Python ‘substring()‘ method is not a built-in method of strings in Python. Instead, you can use string slicing to extract substrings from a string. Here are some examples of when you might use string slicing to extract substrings:

Python
# define a string
s = "Hello, world!"

# extract a portion of the string using string slicing
result = s[7:12]

# print the result
print(result)

Output:

world

In this example, the string slicing notation s[7:12] extracts the substring starting at index 7 and ending at index 12 (not inclusive), which corresponds to the substring “world”.

Reverse a String

We can use substring method for reverse a string. Here is the example:

Python
# define a string
s = "Hello, world!"

# reverse the string using string slicing
result = s[::-1]

# print the result
print(result)

Output:

!dlrow ,olleH

In this example, the string slicing notation ‘s[::-1]‘ reverses the string by slicing it with a step of -1.

Checking if a string contains a substring:

Sometimes we need to check about if a substring contains any string. Then we can also use this method.

Python
# define a string
s = "Hello, world!"

# check if the string contains a substring using the "in" operator
if "world" in s:
    print("Substring found!")
else:
    print("Substring not found.")

Output:

Substring found!

In this example, the "in" operator is used to check if the string ‘s‘ contains the substring “world”.

Share The Tutorial With Your Friends
Twiter
Facebook
LinkedIn
Email
WhatsApp
Skype
Reddit

Check Our Ebook for This Online Course

Advanced topics are covered in this ebook with many practical examples.