Python Trim String [Ultimate Guide]

You’ve probably landed on this article because you’re working on a code project with textual data that has some leading or trailing whitespaces. There’s only one question you want to be answered: How to get rid of those whitespaces? So without wasting any more of your time, let’s get to the core!

How to Trim a Python String?

There are three ways to trim a Python string:

  1. string.strip(): Removes leading and trailing whitespaces including newline characters ‘\n’ and tabular characters ‘\t’.
  2. string.lstrip(): Removes all whitespaces on the left of the string (leading whitespaces).
  3. string.rstrip(): Removes all whitespaces on the right of the string (trailing whitespaces).

All three are built-in string methods called without arguments.

1. string.strip()

So if you simply want to remove all leading and trailing characters, use the following code:

>>> string = "  Finxter is cool \n  \t  "
>>> print(string.strip())
Finxter is cool

Try it yourself in our interactive Python shell:

This removes all whitespaces, both leading and trailing.

You can also remove specific leading and trailing characters by using the optional argument characters:

Optional argument: string.strip(characters)

This will remove specific characters from the left and the right of the string. Consider the following example:

>>> string = "xxxFinxterxxx"
>>> string.strip("x")
'Finxter'

This removes all leading and trailing characters ‘x’ from the original string ‘xxxFinxterxxx’. Note that the character ‘x’ within the original string remains unchanged.

You can also specify multiple characters to be removed. In this case, all of the characters will be stripped:

>>> string = "xyz,,,,,,.....Finxter.. ,,,,,,,xyzxxx"
>>> string.strip("xyz,. ")
'Finxter'

2. string.lstrip()

The name lstrip is a short notation for left strip.

If you want to remove all leading whitespaces (from the left), use the following code:

>>> print(string.lstrip())
Finxter is cool 
  

As a result, there are no leading whitespaces anymore. But the newline character on the right side of the string is still there (and the other whitespaces, even if you cannot see them).

You can also remove specific leading characters by using the optional argument characters:

Optional argument: string.lstrip(characters)

This will remove specific characters from the left of the string. Consider the following example:

>>> string = "xxxFinxterxxx"
>>> string.lstrip("x")
'Finxterxxx'

This removes all leading characters ‘x’ from the original string ‘xxxFinxterxxx’. Note that the character ‘x’ within the original string remains unchanged.

You can also specify multiple characters to be removed. In this case, all of the characters will be stripped:

>>> string = "xyz,,,,,,.....Finxter.. ,,,,,,,xyzxxx"
>>> string.lstrip("xyz,. ")
'Finxter.. ,,,,,,,xyzxxx'

3. string.rstrip()

The name rstrip is a short notation for right strip.

If you want to remove all trailing whitespaces (from the right), use the following code:

>>> print(string.rstrip())
  Finxter is cool

As a result, there are no trailing whitespaces anymore. But the whitespace characters on the left side of the string are still there.

You can also remove specific trailing characters by using the optional argument characters:

Optional argument: string.rstrip(characters)

This will remove specific characters from the right of the string. Consider the following example:

>>> string = "xxxFinxterxxx"
>>> string.rstrip("x")
'xxxFinxter'

This removes all trailing characters ‘x’ from the original string ‘xxxFinxterxxx’. Note that the character ‘x’ within the original string remains unchanged.

You can also specify multiple characters to be removed. In this case, all of the characters will be stripped:

>>> string = "xyz,,,,,,.....Finxter.. ,,,,,,,xyzxxx"
>>> string.rstrip("xyz,. ")
'xyz,,,,,,.....Finxter'

Related Questions

To round up your knowledge of the trim function, let’s consider a few related questions and summarize the article.

How to Trim a String in Python from Left?

Use the string method string.lstrip() to remove all leading whitespaces (from the left). For example, call ' hello world '.lstrip() to get the resulting string 'hello world ' with all whitespaces removed from the left.

How to Trim a String in Python from Right?

Use the string method string.rstrip() to remove all leading whitespaces (from the right). For example, call ' hello world '.rstrip() to get the resulting string ' hello world' with all whitespaces removed from the right.

How to Trim String Characters?

Use the strip functions with the optional character argument. For example, remove all characters 'x' with 'xxxFinxterxxx'.strip('x') to obtain the new string 'Finxter'. Do the same with lstrip('x') and rstrip('x') to remove all characters from the left and from the right, respectively.

Which Whitespaces Does string.strip() remove?

The strip() method removes all whitespace characters when called without the optional argument. There are the most important whitespaces that are removed:

  • space ‘ ‘
  • new line characters ‘\n’
  • carriage return ‘\r’
  • horizontal tab ‘\t’

How to Remove Only Spaces ‘ ‘ with string.strip()?

Simply use the empty space as the optional character argument. For example, use ' \n Finxter \t '.strip(' ') to obtain the result '\n Finxter \t' with only empty spaces removed.

How to Remove All Spaces From a String?

Use the method string.replace(' ', '') to replace all spaces in a string with the empty string ''. This is how you can remove any character in a string.

Where to Go From Here

Being proficient in the use of important Python methods such as the strip() method is critical for your success in coding.

It’s not that hard if you have someone to guide you every step of the way. If you feel like you need someone like that, register for my free “Coffee Break Python” Email course where I give you Python micro lessons every day. It’s the best way to improve on autopilot. My subscribers love it!

Join 10,000+ Python learners (100% Free)!

References