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

Data Science

Topics Covered
  • Business Analytics with R (30 Blogs)
  • Data Science (39 Blogs)
  • Mastering Python (64 Blogs)
  • Decision Tree Modeling Using R (1 Blogs)
SEE MORE

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-widget-banner-bg

Everything You Need to Know about Substring in Python

Published on Sep 24,2019 14 Views

MI-new-launch

myMock Interview Service for Real Tech Jobs

myMock-mobile-banner-bg

myMock Interview Service for Real Tech Jobs

  • Mock interview in latest tech domains i.e JAVA, AI, DEVOPS,etc
  • Get interviewed by leading tech experts
  • Real time assessment report and video recording

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.

Substring

 

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 here for live online training with 24/7 support and lifetime access.

Comments
0 Comments

Browse Categories

Subscribe to our Newsletter, and get personalized recommendations.