How to Get the Substring of a String in Python?

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]
'hello'

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:

helloworld
012345678910
se

Alternatives

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
'hello'
>>> s[0::1]     # 2
'hello world'
>>> s[:5:2]     # 3
'hlo'
>>> s[::2]      # 4
'hlowrd'
>>> s[2:5]      # 5
'llo'
>>> s[2:]       # 6
'llo world'
>>> s[:5]       # 7
'hello'
>>> 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'
print(s[2:5])
# 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'
print(s[start:start+n])
# 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'
print(s[start:])
# 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'
print(s[-N:])
# 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'
print(s[::2])
# 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:

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!