Python
string manipulation
substring
programming
Python strings

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.

Browse interview questions

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:

python
text[start:stop]

start is inclusive and stop is exclusive.

python
text = "Hello, world!"
part = text[7:12]
print(part)

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.

python
text = "Hello, world!"
print(text[:5])
print(text[7:])

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.

python
text = "Hello, world!"
print(text[-6:-1])

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.

python
text = "abcdef"
print(text[::2])
print(text[::-1])

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.

python
1text = "Order ID: 12345"
2start = text.find(":") + 2
3result = text[start:]
4print(result)

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:

python
result = text[2:8]

If you prefer named logic, you can wrap slicing in your own helper function, but the language itself expects slicing syntax.

People sometimes ask about "getting a substring" when they actually want to check whether one string appears inside another.

For membership testing, use in.

python
text = "Hello, world!"
print("world" in text)

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
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track 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.

Browse interview questions

All Rights Reserved.