Python | Split String Before Delimiter

Summary: You can use one of the following methods to split a string before the delimiter –

  • Using split
  • Using string slicing
  • Using regex
  • Using partition
  • Using itertools

Minimal Example

# Given String
chat = "Username: Finxter"
# Method 1
print(chat.split(':')[0])
# Method 2
print(chat[:chat.index(":")])
# Method 3
import re
print(re.match("(.*?):", chat).group().strip(':'))
# Method 4
import itertools
print("".join(itertools.takewhile(lambda x: x!=":", chat)))
# Method 5
print(chat.partition(':')[0])

Problem Formulation

 πŸ“œProblem: Given a string; How will you split the string before the delimiter? The output must only contain the substring before the delimiter.

Example

Let’s visualize the problem with the help of an example:

# Input
text =  "Subscribe to - Finxter"
# Expected Output
Subscribe to

⭐Method 1: Using split()

Approach: First, we will simply split the string 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 first part using list indexing [0] (Indexing starts at 0).

Code:

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

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 starting index of the text until the index of the delimiter. The delimiter will not get printed as the string-slicing method excludes the ending index in the output.

Code:

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

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: The re.match() method searches the text to find the delimiter β€œ-” within it. Then, it groups all the characters before the delimiter. The output here also contains the delimiter β€œ-”. We use the strip('-') function to remove the delimiter from the resultant output.

Code:

import re
text = "Subscribe to - Finxter"
res = re.match("(.*?)-", text).group().strip('-')
print(res)

# Subscribe to

Understanding the pattern (.*?)-
It captures and groups any character (except newline character) with zero or more occurrences. It then finds the characters until β€œ-”

Note: The strip() function is used to  trim the whitespaces on the left and right and return a new string.

🌎Related Read:
Python Regex Match
Python String strip()

⭐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 first element of the tuple (everything before the separator).

Code:

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

🌎Related Read: Python String partition()

⭐Method 5: Using itertools

The takewhile() method from the itertools module in Python accepts an iterable and a predicate. It is used to return the elements of the iterable until the predicate is true. The iteration stops when the predicate for any element is false.

Approach: Therefore, you can use the takewhile method of the itertools module and feed in a lambda function that considers and groups all characters in the given string until the occurrence of the delimiter “-“. Once it reaches the delimiter, the predicate becomes false; hence the iteration stops.

Code:

import itertools
text = "Subscribe to - Finxter"
print("".join(itertools.takewhile(lambda x: x!="-", text)))
# Subscribe to

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.