Everything You Need to Know about Substring in Python

Last updated on Jul 15,2021 74K Views

Everything You Need to Know about Substring in Python

edureka.co

A string is a sequence of Unicode character/s. String consists of a series or sequence of characters that may contain alphanumeric and special characters. In this article, we will understand substring in python and how it works in the following manner:

 

What is a Substring in Python?

In python substring is a sequence of characters within another string, In python, it is also referred to as the slicing of string.

 

Slicing Strings in Python

s= s[ startIndex : endIndex]

Please Note: index start from 0 in a string.

 

s= s[ startIndex :]

# Slicing or substring using start, without end index
s= 'This is to demonstrate substring functionality in python.'
s[5:]
Output : 'is to demonstrate substring functionality in python.'

Returns a sliced string from the string which starts from index position 5 till the end of the string.

 

s= s[: endIndex]

# Slicing or substring without start index
s= 'This is to demonstrate substring functionality in python.'
s[:7]
Output: 'This is'

Returns a sliced string from start to end-index-1.

 

s= s[:]

# Slicing or substring without start or end index
s= 'This is to demonstrate substring functionality in python.'
s[:]
Output: 'This is to demonstrate substring functionality in python.'

Returns the complete string.

 

s= s[index]

# Slicing or substring single character
s= 'This is to demonstrate substring functionality in python.'
s[9]
Output: ‘o’

Return single character.

 

Slicing using start-index, end-index and step

s= s[startIndex : endIndex: step]

# Slicing or substring with start-index, end-index and step.
s= 'This is to demonstrate substring functionality in python.'
s[12:22:1]
Output: ‘emosntrate’

When step size is one it is similar to normal slicing.

# Slicing or substring with start-index, end-index and step.
s= 'This is to demosntrate substring functionality in python.'
s[12:22]
Output: ‘emosntrate’

When step size is 2 then it slices out in steps of 2

# Slicing or substring with start-index, end-index and step.
s= 'This is to demonstrate substring functionality in python.'
s[12:22:2]
Output: ‘eonrt’

For example, with step size 1 sliced string is emosntrate

with step size 2 sliced string is emosntrate, which shows slicing in step size 2.

Similarly, for step size 3 it will be emosntrate.

# Slicing or substring with start-index, end-index and step.
s= 'This is to demonstrated substring functionality in python.'
s[12:22:3]
Output: ‘esre’

 

# Reverse a string using Slicing or substring with negative step.
s= 'This is to demonstrated substring functionality in python.'
s[::-1]
Output: ‘.nohtyp ni ytilanoitcnuf gnirtsbus etartnsomed ot si sihT’

 

Working with negative index

The index in string starts from 0 to 5, alternatively we can use a negative index as well.

# Slicing or substring a string using negative index.
s= 'PYTHON'
s[0:-3]
Output: ‘PYT’

 

# Slicing or substring a string using positive index.
s= 'PYTHON'
s[0:3]
Output: ‘PYT’

 

# Get all substrings of string using list comprehension and string slicing 
s= 'PYTHON'
str=[s[i: j]
for i in range(len(s)) 
      for j in range(i + 1, len(s) + 1)] 
print (str)

Output: ['P', 'PY', 'PYT', 'PYTH', 'PYTHO', 'PYTHON', 'Y', 'YT', 'YTH', 'YTHO', 'YTHON', 'T', 'TH', 'THO', 'THON', 'H', 'HO', 'HON', 'O', 'ON', 'N']

 

# Get all substrings of string using list comprehension and string slicing 
s= 'PYTHON'
str=[s[i: j]
for i in range(len(s)) 
     print (str)

Output: ['P', 'PY', 'PYT', 'PYTH', 'PYTHO', 'PYTHON', 'Y', 'YT', 'YTH', 'YTHO', 'YTHON', 'T', 'TH', 'THO', 'THON', 'H', 'HO', 'HON', 'O', 'ON', 'N']

 

Validate if a substring is present within another string

# Check if string is present within another string using in operator
s= 'This is to demonstrated substring functionality in python.'
print("This" in s)
print("Not present" in s)

Output: 
True
False

In operator will return true if a substring is present within another string else will return false.

 

# Check if string is present within another string using find method
s= 'This is to demonstrated substring functionality in python.'
print(s.find("demonstrate"))
print(s.find("Not present")) 

Output: 
11
-1

Find method will print the index if a substring is present within another string else will return -1 if a string is not present.

With this, we come to an end of this Substring in Python article. I hope you got an idea of slicing and how it helps to create different types of substrings in python.

To get in-depth knowledge on Python along with its various applications, you can enroll now for live online Python training with 24/7 support and lifetime access.

Upcoming Batches For Python Programming Certification Course
Course NameDateDetails
Python Programming Certification Course

Class Starts on 18th May,2024

18th May

SAT&SUN (Weekend Batch)
View Details
Python Programming Certification Course

Class Starts on 22nd June,2024

22nd June

SAT&SUN (Weekend Batch)
View Details
BROWSE COURSES
REGISTER FOR FREE WEBINAR Prompt Engineering Explained