Python | Split String After Delimiter

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

# 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 Formulation

ย ๐Ÿ“œ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:

# 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 split() function returns a list of substrings, we can extract the last part using list indexing [1] (Indexing starts at 0).

Code:

text = "Subscribe to - Finxter"
res = text.split('-')[1]
print(res)
# Finxter

Note: The split() 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 s[start:stop:step] 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 index() 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:

text = "Subscribe to - Finxter"
res = text[text.index("-")+1:]
print(res)

# Finxter

Note:

The index()ย  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 re.match(pattern, string) 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 re.findall("-(.*)", given_string) to match and store all characters that come after the “-“.

Code:

import re

text = "Subscribe to - Finxter"
print(re.findall("-(.*)", text)[0])

# Finxter

๐ŸŒŽRelated Read: Python Regex Match

โญMethod 4: Using partition

The partition() 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:

text = "Subscribe to - Finxter"
res = text.partition('-')[2]
print(res)

# Finxter

๐ŸŒŽRelated Read: Python String partition()

โญMethod 5: Using removeprefix

If you are using Python 3.9 or above then Python facilitates you with the removeprefix method that allows you to remove the substring that comes before a specified substring. Here’s a quick look at how the removeprefix method works –

source: https://docs.python.org/3.9/library/stdtypes.html#str.removeprefix

Code:

text = "Subscribe to - Finxter"
print(text.removeprefix('Subscribe to -'))

# Finxter

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.