Monday, November 18, 2024
Google search engine
HomeLanguagesPython | Rear stray character String split

Python | Rear stray character String split

Python3




# 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




# 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




# 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:

  • Import the re module
  • Initialize the string
  • Use re.split() to split the string by the comma followed by any number of whitespace characters and discard the last element
  • Print the result

Below is the implementation of the above approach:

Python3




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

Python3




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:

  • Initialize the input string test_str.
  • Use the list comprehension to split the string at commas and remove any whitespace before or after each word.
  • Use slicing to remove the last element of the resulting list, which will be an empty string due to the trailing comma in the original string.
  • Store the resulting list of words in the variable res.
  • Print the original string and the resulting list.

Below is the implementation of the above approach:

Python3




# 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.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments