How do I get a substring of a string in Python?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python, the usual way to get a substring is slicing. Python strings behave like sequences, so you can extract part of a string with the familiar [start:stop] syntax instead of calling a dedicated substring method.
That is the main reason the answer can feel surprising to people coming from Java, C#, or JavaScript. Python simply uses sequence slicing for this job.
Basic String Slicing
The general form is:
start is inclusive and stop is exclusive.
This prints world.
Because the stop index is exclusive, character 12 itself is not included.
Omitting Start or Stop
Python lets you omit either side of the slice.
This prints:
- '
Hello' - '
world!'
This is one reason slicing feels so natural in Python. You can express "from the beginning" or "to the end" without extra helper methods.
Negative Indices
Negative indices count from the end of the string.
This again prints world.
That is often useful when you want the last few characters of a filename, extension, or identifier without first calculating the string length.
Slicing with a Step
Python also supports a third slice component called step.
This prints:
- '
ace' - '
fedcba'
The first example takes every second character. The second reverses the string.
This is still substring extraction, just with more control over how characters are selected.
Finding a Dynamic Substring First
Sometimes you do not know the indices in advance. In that case, find them first and then slice.
This prints 12345.
If the substring boundary is pattern-based, regular expressions may be a better fit, but slicing is still the final extraction mechanism in many cases.
There Is No Built-In substring() Method
This is worth stating explicitly: Python strings do not have a substring() method.
The Pythonic way is slicing:
If you prefer named logic, you can wrap slicing in your own helper function, but the language itself expects slicing syntax.
Related But Different: Checking for a Substring
People sometimes ask about "getting a substring" when they actually want to check whether one string appears inside another.
For membership testing, use in.
That returns True.
So the distinction is:
- use slicing to extract a substring
- use
in,find, or regex to search for one
Common Pitfalls
The biggest mistake is expecting a Java-style substring() method in Python. Slicing is the intended tool.
Another common issue is forgetting that the stop index is exclusive. text[7:12] includes index 7 but not index 12.
People also sometimes use negative indices incorrectly and end up excluding the last character by accident. When in doubt, print a few examples and verify the boundaries.
Finally, do not use regex when simple slicing already solves the problem. Start with the simplest tool that matches the task.
Summary
- Use slicing to get substrings in Python.
- The basic form is
text[start:stop]. - The stop index is exclusive.
- Omitted bounds and negative indices make slicing very flexible.
- Use the optional step value for patterns such as every second character or reversed strings.
- Python does not have a built-in
substring()string method.
Related reading
- How do I get a value of datetime.today in Python that is timezone aware?
- How do I get child process PIDs when using ProcessPoolExecuter?
- How do I get current URL in Selenium Webdriver 2 Python?
- How do I get Flask to run on port 80?
- How do I get indices of N maximum values in a NumPy array?
- How do I get indices of N maximum values in a NumPy array?
- How do I get IntelliJ to recognize common Python modules?
- How do I get list of methods in a Python class?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.