3 Easy Ways to Trim a String in Python

Strings are used to represent the series of data, including characters, numbers, symbols, etc. Python provides numerous functions to perform operations on strings, such as extracting substrings, replacing substrings, trimming, etc. Trimming a string means removing or trimming whitespaces and specific characters placed at the leading and trailing sides of the string. 

This post will provide various ways to trim a string in Python using appropriate examples:

Method 1: Using strip() Function

The “strip()” function is utilized to remove any leading and trailing spaces or specific characters from the string. The syntax of this function is shown below:

string.strip(characters)

The parameter character is used to trim/remove specific characters from the string.

Example 1: Trimming Extra Spaces From Leading and Trailing Sides

The below code is used to trim leading and trailing spaces from a string:

Code:

string_value = "     itslinuxfoss  "
print('Given String: ',string_value)
output = string_value.strip()
print("\nString After Trimming Spaces:",output)
  • Firstly, the string value “itslinuxfoss” is initialized with extra spaces.
  • The “strip()” function is utilized to trim the whitespaces from the leading and trailing sides of the given string.

Output: 

Example 2: Trimming Specific Characters From Leading and Trailing Sides

The following code is used to trim specific characters from the leading and trailing sides:

Code:

string_value = "##itslinuxfoss##"
print('Given String: ',string_value)
output = string_value.strip('#')
print("\nString After Trimming Characters:",output)

The “strip()” function takes the specific character value as an argument and trim it from the given string.

Output:

Method 2: Using lstrip() Function

The “lstrip()” function is used to remove the leading spaces and characters from the string. 

Example 1: Trimming Extra Leading Spaces

The following code is used to trim the extra leading spaces:

Code:

string_value = "    itslinuxfoss  "
print("Given String:", string_value)
output = string_value.lstrip()
print("\nString After Trimming:", output)

The “lstrip()” function is used to trim the leading space from the given string named “string_value”.

Output:

Example 2: Trimming Characters From the Leading Sides

The following code is used to trim the specific characters from the leading side of the string:

Code:

string_value = "###itslinuxfoss###"
print("Given String:", string_value)
output = string_value.lstrip('#')
print("\nString After Trimming:", output)

The “lstrip()” function accepts the specific characters “#” as an argument and trim or remove it from the string.

Output:

Method 3: Using rstrip() Function

The “rstrip()” function is used to trim the extra spaces or characters from the trailing side of the given string:

Example 1: Trimming Extra Trailing Spaces

In the below code, the “rstrip()” function is used to trim the extra trailing spaces:

Code:

string_value = "itslinuxfoss   "
print('Given String: ',string_value)
print("Length of Input String:",len(string_value))
output = string_value.rstrip()
print("\nString After Trimming: ", output)
print("Length of Input String: ", len(output))
  • The “rstrip()” function trim the trailing whitespaces from the given string named “string_value”.
  • The “len()” function is utilized to find string length before and after trimming.

Output:

Example 2: Trimming Specific Character From Trailing Side of String

The following code is used to trim the specific character from the trailing side of the given string:

Code:

string_value = "$ itslinuxfoss$$$$"
print('Given String: ',string_value)
print("Length of Input String:",len(string_value))
output = string_value.rstrip('$')
print("\nString After Trimming: ", output)
print("Length of Input String: ", len(output))
  • The specific character “$” is passed as an argument to the “rstrip()” function. The stated function will trim the specific characters from the trailing side of the string.
  • The “len()” function is utilized to find the string length before and after trimming.

Output:

Method 4: Using Numpy Module

The “Numpy” module provides built-in functions named “numpy.char.strip()”, “numpy.char.lstrip()‘ and “numpy.char.rstrip()‘ to remove the trailing and leading whitespaces and characters. 

Example 1: Removing Extra Spaces 

The below code is used to trim the extra spaces from the leading and trailing sides:

Code:

import numpy
array_value = numpy.array(['  Python', 'Java  '])
print("Given Array:", array_value)
output = numpy.char.strip(array_value)
print("\nArray After Trimming From Both Sides:", output)
output1 = numpy.char.lstrip(array_value)
print("\nArray After Trimming From Leading Sides:", output1)
output2 = numpy.char.rstrip(array_value)
print("\nArray After Trimming From Trailing Sides:", output2)
  • The “numpy.array()” function is used to generate the array.
  • The element in an array contains leading and trailing whitespaces.
  • The “numpy.char.strip()” function removes the whitespaces from the leading and trailing sides.
  • Similarly, the “numpy.char.lstrip()” function only removes the whitespace from the leading sides.
  • The “numpy.char.rstrip()” function removes the extra spaces from the trailing sides.

Output:

Example 2: Removing Specific Characters 

The below code is used to remove specific characters from the leading and trailing sides of the given string;

Code:

import numpy
array_value = numpy.array(['##Python##', '##Java##'])
print("Given Array:", array_value)
output = numpy.char.strip(array_value, chars="#")
print("\nArray After Trimming From Both Sides:", output)
output1 = numpy.char.lstrip(array_value, chars="#")
print("\nArray After Trimming From Leading Sides:", output1)
output2 = numpy.char.rstrip(array_value, chars="#")
print("\nArray After Trimming From Trailing Sides:", output2)
  • The “numpy.array()” function is used to generate the array.
  • The “numpy.char.strip()” function removes the specific characters from the leading and trailing sides of the array.
  • The “numpy.char.lstrip()” function removes the specific characters from the leading sides of the array.
  • The “numpy.char.rstrip()” function removes the specific characters from the trailing sides of the array.

Output:

Conclusion

To trim a string, the strip(), lstrip(), and rstrip() functions are used in Python. The built-in functions of the Numpy module can be used to trim a string array. To trim whitespaces and specific characters from the leading and trailing sides, the “strip()” function and “numpy.char.strip()” function of the numpy module is used. Similarly, to remove space or characters, particularly from leading or trailing, the “rstrip()” and “lstrip()” functions are used in Python. 

This guide has presented various ways to trim a string in Python using numerous examples.