A Quick Guide To Split A String On The First Occurrence In Python

Split a string on the first occurrence in Python

Hi guys, today, we will show you how to split a string on the first occurrence in Python in 3 ways. Read the article to find out the best way that works for you.

Split a string on the first occurrence in Python

Below, we will introduce you to 3 useful methods to split a string in Python.

Using the split() function

Learn about syntax and usage of the split() function here.

The requirement is that they must split on the first occurrence of a given string. We will pass a separator used to separate the string into the split() function. By default, if the second argument is not passed, split() will split the string based on all the separators it finds in the string. But if we pass number 1 as the second argument, split() only splits at the first separator position.

This is probably the simplest and easiest way to understand. See the code below to understand more.

my_str = "learn Python programming at at learnshareit.com"
new_str = my_str.split("at ", 1)
print(new_str)

Output:

['learn Python programming ', 'at learnshareit.com']

Using the split() function in Regex library

Syntax:

re.split(pattern, str, maxsplit)

Parameters:

  • pattern: the regular expression.
  • str: a given string.
  • maxsplit: the number of splits in a string.

In this case, we use the split() function in the Regex library. This function is quite similar to using maxsplit in the split() function. The only difference is syntax.

The first thing we have to do is to import the Regex library with a line of code like this:

import re

Then we can use the split() function in re.

import re

my_str = "learn Python programming at at learnshareit.com"
new_str = re.split("at ", my_str, 1)
print(new_str)

Output:

['learn Python programming ', 'at learnshareit.com']

Using the str.partition() function

Syntax:

str.partition(separator)

Description:

The partition() function separates the string at the first occurrence of the separator and then returns a tuple including three parts: before the delimiter, the delimiter itself, and the rest after the delimiter. If the delimiter is not found, it returns a tuple containing three elements: the original string and two blanks after it.

We use partition() to split a string on the first occurrence into a 3-element tuple, then we convert the tuple to a list and remove the separator element, and we’ve solved the problem.

my_str = "learn Python programming at at learnshareit.com"
new_str = my_str.partition("at ")

# Convert tuple to list
str_list = list(new_str)

# Delete separator
del str_list[1]

print(str_list)

Output:

['learn Python programming ', 'at learnshareit.com']

Summary

You just finished reading a quick tutorial on how to split a string on the first occurrence in Python. We hope it is helpful to you. Leave a comment below if you have any questions. Have a nice day!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *