Python Programming (136 Blogs) Become a Certified Professional
AWS Global Infrastructure

Data Science

Topics Covered
  • Business Analytics with R (26 Blogs)
  • Data Science (20 Blogs)
  • Mastering Python (86 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

Everything You Need to Know about Substring in Python

Last updated on Jul 15,2021 74.2K Views


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

  • Slicing using start-index and end-index

s= s[ startIndex : endIndex]

Please Note: index start from 0 in a string.

 

  • Slicing using start-index without end-index

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.

 

  • Slicing using end-index without start-index

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.

 

  • Slice complete string

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.

 

  • Slice single character from a 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 a negative step
# 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

Substring in Python

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’

 

  • The same string can be sliced using a positive index as well
# Slicing or substring a string using positive index.
s= 'PYTHON'
s[0:3]
Output: ‘PYT’

 

  • Retrieving all substrings using list comprehension
# 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']

 

  • Retrieving all substrings using itertools.combinations() method
# 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

  • Using in operator
# 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.

 

  • Using the find method
# 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 30th November,2024

30th November

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

Class Starts on 28th December,2024

28th December

SAT&SUN (Weekend Batch)
View Details
Comments
0 Comments

Join the discussion

Browse Categories

webinar REGISTER FOR FREE WEBINAR
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP

Subscribe to our Newsletter, and get personalized recommendations.

image not found!
image not found!

Everything You Need to Know about Substring in Python

edureka.co