11-30-2022, 12:30 AM
Python | Split String After Delimiter
Rate this post
Summary: You can use one of the following methods to split a string after the delimiter –
Minimal Example
[b]Problem Formulation[/b]
Problem: Given a string; How will you split the string after the delimiter? The output must only contain the substring after the delimiter.
Example
Let’s visualize the problem with the help of an example:
Method 1: Using split()
Approach: First, we will simply split thestring using “-” as the delimiter. Next, to extract the string before the delimiter we will use the index of the required substring. As the function returns a list of substrings, we can extract the last part using list indexing (Indexing starts at 0).
Code:
Note: The function splits the string at a given separator and returns a split list of substrings. It returns a list of the words in the string, using sep as the delimiter string.
Related Read: Python String split()
Method 2: Using String Slicing
Prerequisite: String slicing is a concept of carving out a substring from a given string. Use slicing notation to access every step-th element starting from index start (included) and ending in index stop (excluded). All three arguments are optional, so you can skip them to use the default values.
Approach: First, we will use the method to find the occurrence of the delimiter in the text. Next, we will slice the string from the index next to the index of the delimiter until the end of the string. Note that we are starting from the index of the delimiter+1 because we don’t want to include it in the final output.
Code:
Note:
The method is used to return the index of the first occurrence of the specified substring, like find() but it raises a ValueError if the substring is not found.
Related Reads: String Slicing in Python
Python String index()
Method 3: Using regex
The method returns a match object if the pattern matches at the beginning of the string. The match object contains useful information such as the matching groups and the matching positions.
Approach: Use the expression, given_string) to match and store all characters that come after the ““.
Code:
Related Read: Python Regex Match
Method 4: Using partition
The method searches for a separator substring and returns a tuple with three strings: (1) everything before the separator, (2) the separator itself, and (3) everything after it. It then returns a tuple with the same three strings.
Approach: We have used the partition method and used “-” as a separator. As we only need the substring before the delimiter, we have used the index of the required substring on the returned tuple and just printed the third element of the tuple (everything before the separator).
Code:
Related Read: Python String partition()
Method 5: Using removeprefix
If you are using Python 3.9 or above then Python facilitates you with the method that allows you to remove the substring that comes before a specified substring. Here’s a quick look at how the method works –
source: https://docs.python.org/3.9/library/stdt...moveprefix
Code:
Conclusion
Hurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.
Rate this post
Summary: You can use one of the following methods to split a string after the delimiter –
- Using split
- Using string slicing
- Using regex
- Using partition
- Using removeprefix
Minimal Example
Code:
# Given String
chat = "Python Founder: Guido van Rossum"
# Method 1
print(chat.split(':')[1])
# Method 2
print(chat[chat.index(":")+1:])
# Method 3
import re
print(re.findall(":(.*)", chat)[0])
# Method 4
print(chat.partition(':')[2])
# Method 5
print(chat.removeprefix('Python Founder:'))
Problem: Given a string; How will you split the string after the delimiter? The output must only contain the substring after the delimiter.Example
Let’s visualize the problem with the help of an example:
Code:
# Input
text = "Subscribe to - Finxter"
# Expected Output
Finxter
Method 1: Using split()Approach: First, we will simply split thestring using “-” as the delimiter. Next, to extract the string before the delimiter we will use the index of the required substring. As the
Code:
split()Code:
[1]Code:
Code:
text = "Subscribe to - Finxter"
res = text.split('-')[1]
print(res)
# FinxterNote: The
Code:
split()
Related Read: Python String split()
Method 2: Using String SlicingPrerequisite: String slicing is a concept of carving out a substring from a given string. Use slicing notation
Code:
s[start:stop:step]Approach: First, we will use the
Code:
index()Code:
Code:
text = "Subscribe to - Finxter"
res = text[text.index("-")+1:]
print(res) # FinxterNote:
The
Code:
index()
Related Reads: String Slicing in PythonPython String index()
Method 3: Using regexThe
Code:
re.match(pattern, string)Approach: Use the expression
Code:
re.findall("-(.*)"Code:
-Code:
Code:
import re text = "Subscribe to - Finxter"
print(re.findall("-(.*)", text)[0]) # Finxter
Related Read: Python Regex Match
Method 4: Using partitionThe
Code:
partition()Approach: We have used the partition method and used “-” as a separator. As we only need the substring before the delimiter, we have used the index of the required substring on the returned tuple and just printed the third element of the tuple (everything before the separator).
Code:
Code:
text = "Subscribe to - Finxter"
res = text.partition('-')[2]
print(res) # Finxter
Related Read: Python String partition()
Method 5: Using removeprefixIf you are using Python 3.9 or above then Python facilitates you with the
Code:
removeprefixCode:
removeprefix
source: https://docs.python.org/3.9/library/stdt...moveprefixCode:
Code:
text = "Subscribe to - Finxter"
print(text.removeprefix('Subscribe to -')) # FinxterHurrah! We have successfully solved the given problem using as many as five different ways. I hope you enjoyed this article and it helps you in your Python coding journey. Please subscribe and stay tuned for more interesting articles!
Do you want to master the regex superpower? Check out my new book The Smartest Way to Learn Regular Expressions in Python with the innovative 3-step approach for active learning: (1) study a book chapter, (2) solve a code puzzle, and (3) watch an educational chapter video.

