How to Get the Substring of a String in Python?

5/5 - (1 vote)

To get a substring of a given string in Python, you can use a popular feature called “slicing“. The syntax is string[start:stop:step] with the following meaning:

  • start is the index of the first character included in the substring,
  • stop is the index of the last character, but it is not itself included in the substring, and
  • step is the step size that allows you to skip some characters when creating the substring or to traverse the original string from right to left using a negative step size.

Example Substring Syntax

Here’s an example where you apply this syntax to get the substring 'hello' from the original string 'hello world'.

>>> s = 'hello world'
>>> s[0:5:1]

For ease of understanding, here’s the index table of the string 'hello world'—I’ve marked the start and stop indices with s and e:



You can use slicing in eight different variations to get the the substring of a given string.

Here they are:

  1. string[start:stop:step] – Get substring with given start and stop indices, and step size
  2. string[start::step] – Get substring but stop at string boundary.
  3. string[:stop:step] – Get substring but start at string boundary.
  4. string[::step] – Get substring with default string boundaries for start and stop.
  5. string[start:stop] – Get substring with default step size 1.
  6. string[start:] – Get substring with default step size 1 and stop at string boundary.
  7. string[:stop] – Get substring with default start and stop at string boundaries.
  8. string[::] and string[:] – Get copy of original string.

Take your time and slowly go over all examples, one per alternative—it’ll be time well spent to boost your coding skills!

>>> s = 'hello world'
>>> s[0:5:1]    # 1
>>> s[0::1]     # 2
'hello world'
>>> s[:5:2]     # 3
>>> s[::2]      # 4
>>> s[2:5]      # 5
>>> s[2:]       # 6
'llo world'
>>> s[:5]       # 7
>>> s[::]       # 8
'hello world'

Let’s dive into some practical examples next.

Python Get Substring Between Two Indexes

To get the substring between two indices start (included) and stop (excluded), use the slicing expression string[start:stop]. For example, to get the substring starting with index 2 and ending with index 5 in the original string 'hello world', use the expressiong 'hello world'[2:5].

start, stop = 2, 5
s = 'hello world'
# llo

Python Get Substring By Length

To get a substring of an original string by a given length n and start index, use the slicing expression string[start:start+n]. For example, to get the substring of 'hello world' starting with index 2 and length 5 characters, use the expression 'hello world'[2:2+5] or 'hello world'[2:7].

start = 2
n = 5
s = 'hello world'
# llo w

Python Get Substring From Index to End

To get a substring with a given start index and slice all the way to the right, use the slicing expression string[start:]. For example, to get the substring of 'hello world' starting with index 2, use the expression 'hello world'[2:] that results in 'llo world'.

start = 2
s = 'hello world'
# llo world

๐Ÿ‘‰ Recommended Tutorial: How to Find the Highest Index of a Substring in Python

Python Get Last N Characters From a String

To get the last N characters from a given string, use the slicing expression string[-N:]. For example, to get the last 5 characters of 'hello world', use the expression 'hello world'[-5:] that results in 'world'.

N = 5
s = 'hello world'
# world

Python Get Every Other Character From a String

To get every other character from a given string, use the slicing expression string[::2] setting the step size to 2. For example, to get every other character of 'hello world', use the expression 'hello world'[::2] that results in 'hlowrd'.

s = 'hello world'
# hlowrd

Video Explanation Slicing

In case you need some in-depth explanation on how slicing works, feel free to check out my video guide here:

The Ultimate Guide to Slicing in Python

Where to Go From Here?

Enough theory. Letโ€™s get some practice!

Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people. Thatโ€™s how you polish the skills you really need in practice. After all, whatโ€™s the use of learning theory that nobody ever needs?

You build high-value coding skills by working on practical coding projects!

Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?

๐Ÿš€ If your answer is YES!, consider becoming a Python freelance developer! Itโ€™s the best way of approaching the task of improving your Python skillsโ€”even if you are a complete beginner.

If you just want to learn about the freelancing opportunity, feel free to watch my free webinar โ€œHow to Build Your High-Income Skill Pythonโ€ and learn how I grew my coding business online and how you can, tooโ€”from the comfort of your own home.

Join the free webinar now!