Python | Split String Keep First

5/5 - (2 votes)

🍎Summary: Use given_string.split('sep', 1)[0] to split the string and get the last element. Another approach is to use given_string.partition('sep', 1)[0]

Minimal Example

dob = '21/08/2023'
# Method 1
print(dob.split('/', 1)[0])
# Method 2
print(dob.partition('/')[0])
# OUTPUT: 21

Problem Formulation

📜Problem: Given a string. How will you split the string and get the first element?

Let’s try to understand the given problem with the help of a couple of examples.

Example 1

# Input:
text = 'Java_C++_C#_Golang_Python'
# Expected Output:
Split string list:  ['Java', 'C++_C#_Golang_Python']
First Element:  Java

In the above example, “_” is the separator. However, not the entire string has been split. Only the first substring that comes before the first occurrence of the separator has been extracted.

Example 2

# Input:
text = '_Java_C++_C#_Golang_Python'
# Expected Output:
Split string list:  ['Java', 'C++_C#_Golang_Python']
First Element:  Java

Unlike the previous example, the input string begins with the separator itself. However, the output is similar. So, you have a different input string and you have to get rid of the separator and then get the first element.


Let’s dive into the different ways of solving the given problems.

Method 1: Using split()

Prerequisite:

split() is a built-in function in Python that splits the string at a given separator and returns a split list of substrings.

Syntax: str.split(sep=None, maxsplit=-1)

  • Return a list of the words in the string, using sep as the delimiter string. If maxsplit is given, at most maxsplit splits are done (thus, the list will have at most maxsplit+1 elements). If maxsplit is not specified or -1, then there is no limit on the number of splits (all possible splits are made).
  • If sep is given, consecutive delimiters are not grouped together and are deemed to delimit empty strings (for example, '1,,2'.split(',') returns ['1', '', '2']). The sep argument may consist of multiple characters (for example, '1<>2<>3'.split('<>') returns ['1', '2', '3']). Splitting an empty string with a specified separator returns [''].
  • If sep is not specified or is None, a different splitting algorithm is applied: runs of consecutive whitespace are regarded as a single separator, and the result will contain no empty strings at the start or end if the string has leading or trailing whitespace. Consequently, splitting an empty string or a string consisting of just whitespace with a None separator returns [].

Approach: You can use the maxsplit argument to your advantage and solve the given question by setting maxsplit = 1. This means the string will be split along the specified separator only once, i.e., at the first occurrence of the separator. Once the string is split into two parts all that you need to do is extract the second element from the list using it’s index.

Solution to Example 1:

text = 'Java_C++_C#_Golang_Python'
res = text.split('_', 1)
print("Split string list: ", res)
print("First Element: ", res[0])

Output:

Split string list:  ['Java', 'C++_C#_Golang_Python']
First Element:  Java

Solution to Example 2: In the second scenario, you must get rid of the separator that comes at the beginning of the string. Otherwise, simply using split with the maxsplit argument will create a list that will create a list that will contain an empty character as the last item as shown below –

To avoid this problem, you can use the strip() function to get rid of the separator and then use split as shown in the snippet below.

text = '_Java_C++_C#_Golang_Python'
text = text.strip('_')
res = text.split('_', 1)
print("Split string list: ", res)
print("First Element: ", res[0])

Output:

Split string list:  ['Java', 'C++_C#_Golang_Python']
First Element:  Java

Method 2: 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. 

Thus, you can simply extract the first item from the tuple after the string has been cut by the partition method.

# Solution to Example 1
text = 'Java_C++_C#_Golang_Python'
print("Split String: ", text.partition('_'))
print("First Element: ", text.partition('_')[0])

# Solution to Example 2
text = '_Java_C++_C#_Golang_Python'
text = text.strip('_')
print("Split String: ", text.partition('_'))
print("First Element: ", text.partition('_')[0])

Output:

Split String:  ('Java', '_', 'C++_C#_Golang_Python')
First Element:  Java

Split String:  ('Java', '_', 'C++_C#_Golang_Python')
First Element:  Java

Coding Challenge

Before we wrap up this tutorial, here’s a coding challenge for you to test your grip on the concept you just learned.

Input: Consider the following IP Address –
ip = ‘110.210.130.140’
Challenge: Extract the network bit from the given class A ip address and convert it to its binary form.
Expected Output:
1101110
Hint:
– 110.0.0.0 is the Host ID!
 How to Convert a String to Binary in Python?

Solution:

ip = '110.210.130.140'
nw_bit = int(ip.partition('.')[0])
print(bin(nw_bit)[2:])

# 1101110

Explanation: The solution is pretty straightforward. You first have to extract the network bit in its decimal form, i.e., 110. This happens to be the first item before the “.“. So, you can use partition and feed “.” as the separator and extract the first item (the network bit) from the tuple returned by the partition method. Since this will be a string, you must convert it to an integer and then typecast this integer to a binary number using the bin function to generate the final output.

Conclusion

I hope you enjoyed the numerous scenarios and challenges used in this tutorial to help you learn the two different ways of splitting a string and getting the first item. Please subscribe and stay tuned for more interesting tutorials and solutions.

Recommended Read: Python | Split String and Get Last Element