The Wayback Machine - https://web.archive.org/web/20220708220631/https://www.geeksforgeeks.org/python-get-the-substring-from-given-string-using-list-slicing/
Skip to content
Related Articles

Related Articles

Python | Get the substring from given string using list slicing

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 05 Jul, 2022

Given a string, write a Python program to get the substring from given string using list slicing. Let’s try to get this using different examples.

What is substring?

A substring is a portion of a string. Python offers a variety of techniques for producing substrings, as well as for determining the index of a substring and more.

Syntax of list slicing

Syntax: myString[start:stop:step]

Parameter:

  • start: It is the index of the list where slicing starts.
  • stop: It is the index of the list where slicing ends.
  • step: It allows you to select nth item within the range start to stop.

Example 1: In this example, we will see how to take a substring from the end or from starting of the string. 

Python3




# Python3 code to demonstrate
# to create a substring from a string
 
# Initialising string
ini_string = 'xbzefdgstb'
 
# printing initial string and character
print("initial_strings : ", ini_string)
 
# creating substring from start of string
# define length upto which substring required
sstring_strt = ini_string[:2]
sstring_end = ini_string[3:]
 
# printing result
print("print resultant substring from start", sstring_strt)
print("print resultant substring from end", sstring_end)

Output:

initial_strings :  xbzefdgstb
print resultant substring from start xb
print resultant substring from end efdgstb

Example 2: In this example, we will see how to create a string by taking characters from a certain positional gap. 

Python3




# Python3 code to demonstrate
# to create a substring from string
 
# Initialising string
ini_string = 'xbzefdgstb'
 
# printing initial string and character
print("initial_strings : ", ini_string)
 
# creating substring by taking element
# after certain position gap
# define length upto which substring required
sstring_alt = ini_string[::2]
sstring_gap2 = ini_string[::3]
 
# printing result
print("print resultant substring from start", sstring_alt)
print("print resultant substring from end", sstring_gap2)

Output:

initial_strings :  xbzefdgstb
print resultant substring from start xzfgt
print resultant substring from end xegb

Example 3: In this example, we are considering both cases of taking strings from the middle with some positional gap between characters. 

Python3




# Python3 code to demonstrate
# to create a substring from string
 
# Initialising string
ini_string = 'xbzefdgstb'
 
# printing initial string and character
print ("initial_strings : ", ini_string)
 
# creating substring by taking element
# after certain position gap
# in defined length
sstring = ini_string[2:7:2]
 
# printing result
print ("print resultant substring", sstring)

Output:

initial_strings :  xbzefdgstb
print resultant substring zfg

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!