Split a string only on the first Space in Python

Split a string only on the first Space in Python

If you are looking for how to split a string only on the first Space in Python, do not miss our article. Today, we will give you two methods to do that by both the split() function and a customized function.

How to split a string only on the first space in Python?

First, we will show you how to use the split() function to split a string only on the first space in Python.

Using the split() function

The split() function can separate a string by a specified delimiter and return a list containing substrings as its elements. The delimiter can be anything that depends on your target. 

Syntax:

string.split(sep, maxsplit)

Parameter:

  • sep: The delimiter to determine the position to split. The default value is whitespace.
  • maxsplit: Number of split times. The default value is -1 to split all delimiter positions.

To split a string by the space, use the sep is ” ” and the maxsplit is 1.

Code:

string = "Learn Share IT"
 
# Split the string on the first space
string = string.split(sep = " ", maxsplit = 1)
 
print(f"The string after splitting is: \n{string}")

Result:

The string after splitting is: 
['Learn', 'Share IT']

Because the default value of the sep is whitespace, you can only pass value to the maxsplit like below to get the same result:

string = string.split(maxsplit = 1)

Sometimes, your string may have space at the beginning, and you can get a bad result if you only use the split() function. To handle this problem, use the strip() function for your string to eliminate unnecessary space before using the split() function. Look at the following example:

string = ' Learn Share IT'
 
# The unexpected result after splitting
unexpectedResult = string.split(sep = ' ', maxsplit = 1)
print(unexpectedResult)
 
print("-----------------")
 
# The expected result after splitting
expectedResult = string.strip().split(maxsplit = 1)
print(expectedResult)

Result:

[", 'Learn Share IT']
-----------------
['Learn', 'Share IT']

Using a customized function 

In this part, we will create a function to separate a string on the first space. We will traverse the string, find the first whitespace, and break the loop after getting the position. We also create a list to store substrings after splitting the original string.

Code:

string = 'Learn Share IT'
result = []
 
# Traverse the string to find the Space
for i in range(len(string)):  
# Append the first and second parts of the string into the list 
# then break the loop to avoid splitting at other spaces
    if string[i] ==" ":
        result.append(string[:i])
        result.append(string[i:])
        break
        
print(result)

Result:

['Learn', 'Share IT']

Summary

In summary, you can split a string only on the first space in Python by passing the value 1 to the maxsplit. If your string has whitespaces at the beginning, use the strip() function to eliminate them, then apply the split() function to get the expected result. Good luck to you!

Maybe you are interested:

Leave a Reply

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