When working with strings in Python, I got a requirement to split a string at specific indices. In this tutorial, I will walk you through different methods to split a string by index in Python with complete code and examples.
To split a string by index in Python using list comprehensions and slicing, you can leverage the zip function to pair start and end indices, then slice the string accordingly. For example, given the string s = “JohnDoe123MainStreet” and indices [4, 7, 10], you can use the following code: [s[start:end] for start, end in zip(indices, indices[1:] + [None])], which will output [‘John’, ‘Doe’, ‘123’, ‘MainStreet’].
Split a String by Index in Python
There are various methods to split a string by index in Python, let me show you each method with examples.
Method 1: Using List Comprehensions and Slicing
One of the simplest ways to split a string by index in Python is by using list comprehensions along with string slicing. This method uses Python’s strong support for list and string operations.
Syntax:
Here is the syntax:
[s[start:end] for start, end in zip(indices, indices[1:] + [None])]Example:
Let’s say we have a string s = "JohnDoe123MainStreet" and we want to split it at indices [4, 7, 10].
s = "JohnDoe123MainStreet"
indices = [4, 7, 10]
# Using list comprehension and slicing
substrings = [s[start:end] for start, end in zip([0] + indices, indices + [None])]
print(substrings)Output:
['John', 'Doe', '123', 'MainStreet']Here is the output you can see in the screenshot below:

Check out How to Split Last Element in Python
Method 2: Using a Loop with Slicing
Another approach is to use a loop to iterate through the indices and slice the string accordingly.
Syntax:
Here is the syntax:
result = []
for i in range(len(indices) - 1):
result.append(s[indices[i]:indices[i+1]])
result.append(s[indices[-1]:])Example:
Here is an example.
Using the same string s = "JohnDoe123MainStreet" and indices [4, 7, 10]:
s = "JohnDoe123MainStreet"
indices = [4, 7, 10]
# Using a loop with slicing
result = []
prev_index = 0
for index in indices:
result.append(s[prev_index:index])
prev_index = index
result.append(s[prev_index:])
print(result)Here is the exact output you can see in the screenshot below:

['John', 'Doe', '123', 'MainStreet']Check out How to Split Sentence into Words in Python
Method 3: Using Regular Expressions
Regular expressions (regex) can also be used to split a string at specific positions in Python. This method is more flexible and powerful, especially for complex patterns.
Syntax:
Here is the syntax:
import re
re.split(r'(.{4})(.{3})(.{3})(.*)', s)[1:-1]Example:
Here is an example.
Again, using the string s = "JohnDoe123MainStreet":
import re
s = "JohnDoe123MainStreet"
pattern = r'(.{4})(.{3})(.{3})(.*)'
# Using regex to split the string
substrings = re.split(pattern, s)[1:-1]
print(substrings)You can see the output in the screenshot below:
['John', 'Doe', '123', 'MainStreet']Here is the exact output in the screenshot below:

Read Python Split String to Array
Method 4: Using Custom Function
You can also create a custom function to split string by index in Python.
Syntax:
Here is the syntax:
def split_string_by_indices(s, indices):
result = []
prev_index = 0
for index in indices:
result.append(s[prev_index:index])
prev_index = index
result.append(s[prev_index:])
return resultExample:
Here is an example.
Using the same string and indices:
def split_string_by_indices(s, indices):
result = []
prev_index = 0
for index in indices:
result.append(s[prev_index:index])
prev_index = index
result.append(s[prev_index:])
return result
s = "JohnDoe123MainStreet"
indices = [4, 7, 10]
# Using custom function
substrings = split_string_by_indices(s, indices)
print(substrings)Output:
['John', 'Doe', '123', 'MainStreet']Here is the output in the screenshot below:

Check out How to Compare Strings in Python?
Split String at Index in Python
When you need to split a string at specific indices in Python, there are several methods you can use. Here, I’ll explain two effective methods: using list comprehensions with slicing and using a loop with slicing.
Method 1: Using List Comprehensions and Slicing
This method leverages list comprehensions and the zip function to pair start and end indices, allowing you to slice the string accordingly.
Example:
Let me show you an example.
Let’s say you have the string s = "JohnDoe123MainStreet" and you want to split it at indices [4, 7, 10].
s = "JohnDoe123MainStreet"
indices = [4, 7, 10]
# Using list comprehension and slicing
substrings = [s[start:end] for start, end in zip([0] + indices, indices + [None])]
print(substrings)Output:
['John', 'Doe', '123', 'MainStreet']Here is the exact output in the screenshot below:

Method 2: Using a Loop with Slicing
Another approach is to use a loop to iterate through the indices and slice the string at each specified point.
Example:
Let me show you an example.
Using the same string s = "JohnDoe123MainStreet" and indices [4, 7, 10]:
s = "JohnDoe123MainStreet"
indices = [4, 7, 10]
# Using a loop with slicing
result = []
prev_index = 0
for index in indices:
result.append(s[prev_index:index])
prev_index = index
result.append(s[prev_index:])
print(result)Output:
['John', 'Doe', '123', 'MainStreet']Check out How to Convert String to List in Python?
Python Split String By Position
When you need to split a string by specific positions in Python, there are several methods to achieve this. Here, I’ll explain two effective methods: using list comprehensions with slicing and using a regular expression.
Method 1: Using List Comprehensions and Slicing
This method utilizes list comprehensions and the zip function to pair start and end positions, allowing you to slice the string accordingly.
Example:
Here is an example.
Suppose you have the string s = "JohnDoe123MainStreet" and you want to split it at positions [4, 7, 10].
s = "JohnDoe123MainStreet"
positions = [4, 7, 10]
# Using list comprehension and slicing
substrings = [s[start:end] for start, end in zip([0] + positions, positions + [None])]
print(substrings)Output:
['John', 'Doe', '123', 'MainStreet']Method 2: Using Regular Expressions
Regular expressions (regex) provide a powerful way to split a string based on specific patterns or positions. This method is particularly useful for more complex splitting logic.
Example:
Using the same string s = "JohnDoe123MainStreet" and splitting it at positions [4, 7, 10]:
import re
s = "JohnDoe123MainStreet"
pattern = r'(.{4})(.{3})(.{3})(.*)'
# Using regex to split the string
substrings = re.split(pattern, s)[1:-1]
print(substrings)Output:
['John', 'Doe', '123', 'MainStreet']Conclusion
In this tutorial, I explained how to split strings by index in Python using different methods like list comprehensions, loops, regular expressions, or custom functions, etc. For each method, I provided a few examples.
You may also like:
- Find the First Number in a String in Python
- How to Reverse a String in Python?
- Check if a Python String Contains a Substring
- Concatenate String and Float in Python

I am Bijay Kumar, a Microsoft MVP in SharePoint. Apart from SharePoint, I started working on Python, Machine learning, and artificial intelligence for the last 5 years. During this time I got expertise in various Python libraries also like Tkinter, Pandas, NumPy, Turtle, Django, Matplotlib, Tensorflow, Scipy, Scikit-Learn, etc… for various clients in the United States, Canada, the United Kingdom, Australia, New Zealand, etc. Check out my profile.