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:
startis the index of the first character included in the substring,
stopis the index of the last character, but it is not itself included in the substring, and
stepis 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
>>> 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:
You can use slicing in eight different variations to get the the substring of a given string.
Here they are:
string[start:stop:step]– Get substring with given start and stop indices, and step size
string[start::step]– Get substring but stop at string boundary.
string[:stop:step]– Get substring but start at string boundary.
string[::step]– Get substring with default string boundaries for start and stop.
string[start:stop]– Get substring with default step size 1.
string[start:]– Get substring with default step size 1 and stop at string boundary.
string[:stop]– Get substring with default start and stop at string boundaries.
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
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
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
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
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
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
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.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com that has taught exponential skills to millions of coders worldwide. He’s the author of the best-selling programming books Python One-Liners (NoStarch 2020), The Art of Clean Code (NoStarch 2022), and The Book of Dash (NoStarch 2022). Chris also coauthored the Coffee Break Python series of self-published books. He’s a computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.