Python Program to Get a Substring of a String

To understand this example, you should have the knowledge of the following Python programming topics:


Using String slicing

my_string = "I love python."

# prints "love"
print(my_string[2:6])

# prints "love python."
print(my_string[2:])

# prints "I love python"
print(my_string[:-1])

Output

love
love python.
I love python

String slicing works similar to list slicing. The working of above code can be understood in the following points.

  • [2:6]
    You need to specify the starting index and the ending index of the substring. In this case, love starts at index 2 and ends at index 6.
  • [2:]
    All the text from index 2 to the end are selected.
  • [:-1]
    All the text before the last index is selected.

Also Read:

Did you find this article helpful?

Your builder path starts here. Builders don't just know how to code, they create solutions that matter.

Escape tutorial hell and ship real projects.

Try Programiz PRO
  • Real-World Projects
  • On-Demand Learning
  • AI Mentor
  • Builder Community