How To Trim Whitespace from a String in Python

In this Python tutorial, we will learn how to trim a string in Python using the different examples:

There are 5 ways to trim a string in Python, which are shown below:

  • Using strip() function
  • Using the lstrip() method
  • Using the strip() method
  • Using regular expressions
  • Using replace() method

How to trim a string in Python

Trimming a string refers to removing any leading or trailing whitespace characters or other specified characters from a given string.

In Python, there are several ways to trim a string, including using string methods such as strip(), lstrip(), and rstrip(), regular expressions, replace() method and etc.

Method-1: How to trim a string in Python using strip() function

The strip() method removes any leading or trailing whitespace characters (spaces, tabs, newlines) from a string.

# Define a string variable
cities_of_USA = ' New York, Los Angeles, California '

# Print the original string
print("Input String :",cities_of_USA )

# Use the str.strip() function to remove leading and trailing whitespaces from the string
new_output = cities_of_USA.strip()

# Print the string after trimming whitespaces
print(" Input String after trimming :", new_output)

The above code removes whitespace characters from a string using the strip() method in Python.

  • It creates a string called cities_of_USA, which contains a list of cities separated by commas and surrounded by whitespace characters.
  • After that, the strip() method is called on the cities_of_USA variable. This method removes all whitespace characters from the beginning and end of the string but does not remove whitespace characters between words.
  • Finally, the code prints the new string using the print() function and the variable new_output.
Output: Input String :  New York, Los Angeles, California 
 Input String after trimming : New York, Los Angeles, California

Read: Python format number with commas

Method-2: How to trim a string in Python using the lstrip() method

The lstrip() method removes any leading whitespace characters from a string.

# Define a string with leading whitespaces
string = " United Kingdom "

# Use the string method lstrip() to remove leading whitespaces
trimmed_text = string.lstrip()

# Print the trimmed string
print(trimmed_text)

The above code initializes a string variable named “string” with the value ” United Kingdom “. The string contains whitespace characters at the beginning and end.

  • Next, the string method “lstrip()” is applied to the “string” variable. The lstrip() method removes any whitespace characters from the beginning of the string. The resulting trimmed string is assigned to a new variable named “trimmed_text”.
  • Finally, the trimmed string is printed to the console using the print() function.
How to trim a string in Python
How to trim a string in Python

Read: Python string formatting

Method-3: How to trim a string in Python using the rstrip()

The rstrip() method removes any trailing whitespace characters from a string.

# Define a string with leading whitespaces
string = " United Kingdom "

# Use the string method rstrip() to remove trailing whitespaces
trimmed_text = string.rstrip()

# Print the trimmed string
print(trimmed_text)

The above code initializes a string variable named “string” with the value ” United Kingdom “. The string contains whitespace characters at the beginning and end.

  • Next, the string method “rstrip()” is applied to the “string” variable. The rstrip() method removes any whitespace characters from the end of the string. The resulting trimmed string is assigned to a new variable named “trimmed_text”.
  • Finally, the trimmed string is printed to the console using the print() function.
Output:  United Kingdom

Read: Python split string by space

Method-4: How to trim a string in Python using regular expressions

Another way to trim a string in Python is by using regular expressions. The re module in Python provides functions for working with regular expressions.

# Import the regular expression (re) module
import re

# Define a string with leading and trailing whitespace
text = " USA "

# Remove the leading and trailing whitespace using the sub() method of the re module
trimmed_text = re.sub(r'^\s+|\s+$', '', text)

# Print the resulting string
print(trimmed_text)

The above code performs string trimming using the re module. First, the string ” USA “ is assigned to the variable text.

  • Then, the re.sub() function is used to remove any leading or trailing white space from the string. The regular expression r’^\s+|\s+$’ is used as the pattern to match leading and trailing white space characters.
  • The resulting trimmed string is then printed using the print() function.
Output: USA

Read: Python find number in String

Method-5: How to trim a string in Python using replace() method

Another way to trim a string in Python is to use the replace() method. The replace() method replaces all occurrences of a specified substring with another string.

# Define a string variable with leading and trailing whitespace
text = "   USA    "

# Use the replace() method to remove all spaces from the string
trimmed_text = text.replace(" ", "")

# Print the trimmed string
print(trimmed_text)  # Output: "USA"

The above code creates a string variable text with leading and trailing whitespace. The whitespace consists of three spaces before the string “USA” and three spaces after the string.

  • The second line uses the replace() method to remove all spaces from the string. The replace() method takes two arguments: the substring to be replaced (in this case, a space character " ") and the string to replace it with (in this case, an empty string “”).
  • The replace() method returns a new string with all occurrences of the specified substring replaced.
  • The result of the replace() method is assigned to a new variable trimmed_text. The third line prints the trimmed string.
Output: USA

You may also like to read the following Python tutorials.

In this Python tutorial, we have covered how to trim the string using the following methods:

  • Using strip() function
  • Using the lstrip() method
  • Using the rstrip()
  • Using regular expressions
  • Using replace() method