How to Get the substring from given string using list slicing in Python?

A substring is a portion of a string. There are several methods in Python for creating a substring and checking if a substring exists in a string, the index of a substring, and more. Let’s take a look at some substring operations.

Slicing

You can extract a substring from a string by slicing with indices that correspond to your substring.

Syntax:

gvn_string[start:stop:step]

Parameters

  • start: Slicing begins at this index of the list.
  • stop: The index of the list at which slicing ends.
  • step: It allows you to select the nth item from start to stop. A number indicating the step of slicing.  The default value is 1.

Python Program to Get the substring from a given string using list slicing

Method #1: Slicing with start and stop values

Approach:

  • Give the string as static input and store it in a variable.
  • Print the given string
  • Create a substring from the start of the string till the required index you want
  • Create a substring from the end of the string
  • Print the substring from the start of the string using slicing.
  • Here printing characters from 0 to 2(excluding the last index).
  • Print the substring from the end of the string using slicing
  • Here printing characters from 4 to the entire string.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'hellothisispythonprograms'

# Print the given string 
print("The given string = ", gvn_str)

# create a substring from the start of string till the required index you want 
substr_frm_start = gvn_str[:3]
# create a substring from the end of string 
substr_frm_end = gvn_str[4:]

# Print the substring from the starting of the string using slicing
# here printing charcters from 0 to 2(excluding the last index) 
print("The substring from the starting of the string = ", substr_frm_start)
# Print the substring from the end of the string using slicing
# here printing characters from 4 to entire string
print("The substring from the end of the string = ", substr_frm_end)

Output:

The given string = hellothisispythonprograms
The substring from the starting of the string = hel
The substring from the end of the string = othisispythonprograms

Method #2: Slicing with a step value

Approach:

  • Give the string as static input and store it in a variable.
  • Print the given string
  • Get the substring by taking step value as 3 to the slicing
  • Get the alternative character substring by taking step value as 2
  • Print the substring from the start of the given string with 3 step
  • Print the substring from the given string with alternate characters(step=2).
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'hellothisispythonprograms'
# Print the given string 
print("The given string = ", gvn_str)

# Get the substring by taking step value as 3 to the slicing
substr_withstep3 = gvn_str[::3]
# Get the alternative character substring by taking step value as 2
substr_withstep2 = gvn_str[::2]

# print the substring from the start of the given string with 3 step
print("The substring from the start of the given string with 3 step = ", substr_withstep3)
# print the substring from the given string with alternate characters(step=2)
print("The substring from the given string with alternate characters(step=2)= ", substr_withstep2)

Output:

The given string = hellothisispythonprograms
The substring from the start of the given string with 3 step = hlhiyorrs
The substring from the given string with alternate characters(step=2)= hlohssyhnrgas

Method #3: Slicing with start, stop and step values

Approach:

  • Give the string as static input and store it in a variable.
  • Print the given string
  • Slice from 2nd index to 7 index with alternate characters(because we used step value as 2)
  • Print the resulting substring.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = 'hellothisispythonprograms'

# Print the given string 
print("The given string = ", gvn_str)

# Slice from 2nd index to 7 index with alternate characters(because we used step value as 2)
sub_string = gvn_str[2:7:2]

# Print the result substring 
print ("The result substring = ", sub_string)

Output:

The given string = hellothisispythonprograms
The result substring = loh

Method #4: Slicing with start, stop and step values (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Print the given string
  • Give the start value as user input using the int(), input() functions and store it in another variable (here int() converts to an integer datatype)
  • Give the end value as user input using the int(), input() functions and store it in another variable
  • Give the step value as user input using the int(), input() functions
    and store it in another variable
  • Slice the given string based on the above-given start, end and step values and store it in another variable
  • Print the resulting substring.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Give some random string = ")

# Print the given string 
print("The given string = ", gvn_str)

# Give the start value as user input using the int(), input() functions
# and store it in another variable (here int() converts to an integer datatype)
gvn_startval = int(input("Give some random start value = "))
# Give the end value as user input using the int(), input() functions
# and store it in another variable 
gvn_endval = int(input("Give some random end value = "))
# Give the step value as user input using the int(), input() functions
# and store it in another variable 
gvn_step = int(input("Give some random step value = "))

# Slice the given string based on the above given start, end and step values  
# and store it in another variable 
sub_string = gvn_str[gvn_startval:gvn_endval:gvn_step]

# Print the result substring 
print ("The result substring = ", sub_string)

Output:

Give some random string = good morning python programs
The given string = good morning python programs
Give some random start value = 1
Give some random end value = 15
Give some random step value = 2
The result substring = odmrigp

Explanation:

Here the string "good morning python programs" is sliced from the 
start index=1(o) to the end index 15(t) with a step value =2 i.e, 
it prints alternative characters

Method #5: Slicing with a step value (User Input)

Approach:

  • Give the string as user input using the input() function and store it in a variable.
  • Print the given string
  • Give the step value as user input using the int(), and input() functions and store it in another variable (here int() function converts to an integer datatype).
  • Get the substring by taking the given step value to perform slicing.
  • Print the substring from the input string based on the given step value.
  • The Exit of the Program.

Below is the implementation:

# Give the string as user input using the input() function and store it in a variable.
gvn_str = input("Give some random string = ")
# Print the given string 
print("The given string = ", gvn_str)

# Give the step value as user input using the int(), input() functions
# and store it in another variable (here int() function converts to an integer datatype)
gvn_step = int(input("Give some random step value = "))

# Get the substring by taking the given step value to perform slicing
substr_withstep = gvn_str[::gvn_step]

# print the substring from the input string based on the given step value 
print("The substring from the input string based on the given step value  = ", substr_withstep)

Output:

Give some random string = hellothisispythonprograms
The given string = hellothisispythonprograms
Give some random step value = 2
The substring from the input string based on the given step value = hlohssyhnrgas