Remove Whitespace from String in Python

By Chandrashekhar Fakirpure

Updated on Feb 06, 2024

In this tutorial, we'll explain how to remove whitespace from string in Python. We can remove whitespace like spaces, tabs, and newline characters from the beginning and end of a string using the strip() method. 

Whitespace includes all Unicode whitespace characters, such as spaces, tabs (\t), carriage returns (\r), and newlines (\n). Using string() class, we can trim the string. There are 3 methods strip() class has. 

  1. string(): This method use when we want to remove whitespaces from the beginning and end of the string.
  2. lstrip(): This method use when we want to remove whitespaces from the beginning of the string.
  3. rstrip():This method use when we want to remove whitespaces from the end of the string.

Trim whitespace from a String

Here is the example of trim whitespace from a string in Python:

# Example string with whitespace
string_with_whitespace = "   Hello, world!   "

# Using strip() to remove whitespace from both ends
trimmed_string = string_with_whitespace.strip()

print("Trimmed string:", trimmed_string)

# Example string with whitespace only at the beginning
string_with_whitespace_at_beginning = "   Hello, world!"

# Using lstrip() to remove whitespace from the beginning
trimmed_string_beginning = string_with_whitespace_at_beginning.lstrip()

print("Trimmed string (beginning):", trimmed_string_beginning)

# Example string with whitespace only at the end
string_with_whitespace_at_end = "Hello, world!   "

# Using rstrip() to remove whitespace from the end
trimmed_string_end = string_with_whitespace_at_end.rstrip()

print("Trimmed string (end):", trimmed_string_end)

Output:

Trimmed string: Hello, world!
Trimmed string (beginning): Hello, world!
Trimmed string (end): Hello, world!

Trim a specific whitespace character from string

We can also remove only a character from the begining and end of the string. Here is an example of how we can do it:

s3 = '\n  Hello\n  World\t  '

trimmed_newline_char = s3.lstrip('\n')
print(f"remove only leading newline: '{trimmed_newline_char}'")

Output:

remove only leading newline: '  Hello
  World      '

Here we have used lstripe and removed begining char \n from the string.

Remove duplicate spaces and newline

Using join() and split() methods, we can remove duplicate spaces and newline characters. The split() method break up the string into a list, using the default separator of any whitespace character. The join() method joins the list back into one string with a single space (" ") between each word.

Here is the example code:

string_with_whitespace = "\tHello, \n world \r from \nHostnExtra  "

removed_spaces = " ".join(string_with_whitespace.split())

print(removed_spaces)

Output:

Hello, world from HostnExtra

As shown in the example, strip(), lstrip(), and rstrip() methods effectively remove whitespace from strings, allowing you to clean up your data as needed.  We have seen how to remove whitespace from string in Python.