Open In App

Python | Rear stray character String split




# Python3 code to demonstrate working of
# Rear stray character String split
# Using list comprehension
 
# initializing string
test_str = 'gfg, is, best, '
 
# printing original string
print("The original string is : " + test_str)
 
# Rear stray character String split
# Using list comprehension
res = [word.strip() for word in test_str.split(',')][:-1]
 
# printing result
print("The evaluated result is : " + str(res))

Output
The original string is : gfg, is, best, 
The evaluated result is : ['gfg', 'is', 'best']

Sometimes, while working with Python Strings, we can have problem in which we need to split a string. But sometimes, we can have a case in which we have after splitting a blank space at rear end of list. This is usually not desired. Lets discussed ways in which this can be avoided. 



Method #1: Using split() + rstrip() The combination of above functions can be used to solve this problem. In this, we remove the stray character from the string before split(), to avoid the empty string in split list. 




# Python3 code to demonstrate working of
# Rear stray character String split
# Using split() + rstrip()
 
# initializing string
test_str = 'gfg, is, best, '
 
# printing original string
print("The original string is : " + test_str)
 
# Rear stray character String split
# Using split() + rstrip()
res = test_str.rstrip(', ').split(', ')
 
# printing result
print("The evaluated result is : " + str(res))

Output : 

The original string is : gfg, is, best,
The evaluated result is : ['gfg', 'is', 'best']

The time complexity of this program is O(n), where n is the length of the input string. 

The auxiliary space complexity of this program is O(n), where n is the length of the input string.

Method #2: Using split() The use of rstrip() can be avoided by passing additional arguments while performing the split(). 




# Python3 code to demonstrate working of
# Rear stray character String split
# Using split()
 
# initializing string
test_str = 'gfg, is, best, '
 
# printing original string
print("The original string is : " + test_str)
 
# Rear stray character String split
# Using split()
res = test_str.split(', ')[0:-1]
 
# printing result
print("The evaluated result is : " + str(res))

Output : 
The original string is : gfg, is, best,
The evaluated result is : ['gfg', 'is', 'best']

Time Complexity: O(n) -> (string split)
Auxiliary Space: O(n)

Method #3: Using regular expressions. 

This program takes a string, removes the trailing comma and any whitespace characters after it, and then splits the string into a list of substrings using regular expressions. Finally, it prints the list of substrings.

Step-by-step approach:

Below is the implementation of the above approach:




import re
 
# initializing string
test_str = 'gfg, is, best, '
 
# printing original string
print("The original string is : " + test_str)
 
# Rear stray character String split
# Using regular expressions
res = re.split(',\s*', test_str)[:-1]
 
# printing result
print("The evaluated result is : " + str(res))

Output
The original string is : gfg, is, best, 
The evaluated result is : ['gfg', 'is', 'best']

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), because the re.split() method creates a list of substrings, which uses additional memory.

Method #4: Using numpy:

Algorithm:

  1. Import numpy as np
  2. Initialize string test_str
  3. Print the original string
  4. Use numpy.split() to split the string
  5. Convert the result to list using tolist()
  6. Print the result




import numpy as np
 
# initializing string
test_str = 'gfg, is, best, '
 
# printing original string
print("The original string is : " + test_str)
 
# Rear stray character String split
# Using numpy.split()
res = np.array(test_str.split(', ')).astype(str)
res = np.split(res, [len(res) - 1])[0].tolist()
 
# printing result
print("The evaluated result is : " + str(res))
#This code is contributed by Jyothi pinjala

Output:
The original string is : gfg, is, best, 
The evaluated result is : ['gfg', 'is', 'best']

Time Complexity:
The time complexity of the code is O(n), where n is the length of the input string.

Auxiliary Space:
The space complexity of the code is O(n), where n is the length of the input string. This is because we are using the numpy library which requires additional memory space to store the array.

Method 5: Using List Comprehension

Step-by-step approach:

Below is the implementation of the above approach:




# Python3 code to demonstrate working of
# Rear stray character String split
# Using list comprehension
 
# initializing string
test_str = 'gfg, is, best, '
 
# printing original string
print("The original string is : " + test_str)
 
# Rear stray character String split
# Using list comprehension
res = [word.strip() for word in test_str.split(',')][:-1]
 
# printing result
print("The evaluated result is : " + str(res))

Output
The original string is : gfg, is, best, 
The evaluated result is : ['gfg', 'is', 'best']

Time complexity: O(n), where n is the length of the input string.
Auxiliary space: O(n), for the resulting list.


Article Tags :