// Tutorial //

How To Trim Whitespace from a String in Python

Published on August 3, 2022 · Updated on January 31, 2023
Default avatar

By Pankaj

How To Trim Whitespace from a String in Python

Introduction

Python provides three methods that you can use to trim whitespace from a string and return a new string object. The string strip methods can trim leading whitespace, trailing whitespace, or both. To learn more about removing whitespace, including how to remove all whitespace or only duplicate spaces, refer to How To Remove Spaces from a String In Python.

Whitespace includes all Unicode whitespace characters, such as spaces, tabs (\t), carriage returns (\r), and newlines (\n). The Python str() class has the following methods that you can use to trim whitespace from a string:

  • strip([chars]): Trims characters from both ends of a string. When chars is omitted or None, returns a new string with all leading and trailing whitespace removed.
  • rstrip([chars]): Trims characters from the right side of a string. When chars is omitted or None, returns a new string with all trailing whitespace removed.
  • lstrip([chars]): Trims characters from the left side of a string. When chars is omitted or None, returns a new string with all leading whitespace removed.

Trimming Whitespace from a String Using Strip Methods

The following example demonstrates how to trim leading spaces, trailing spaces, and both leading and trailing spaces from a string:

s1 = '  shark  '
print(f"string: '{s1}'")

s1_remove_leading = s1.lstrip()
print(f"remove leading: '{s1_remove_leading}'")

s1_remove_trailing = s1.rstrip()
print(f"remove trailing: '{s1_remove_trailing}'")

s1_remove_both = s1.strip()
print(f"remove both: '{s1_remove_both}'")

The output is:

string: '  shark  '
remove leading: 'shark  '
remove trailing: '  shark'
remove both: 'shark'

The following example demonstrates how to use the same strip methods to trim multiple whitespace characters from a string:

s2 = '  \n shark\n squid\t  '
print(f"string: '{s2}'")

s2_remove_leading = s2.lstrip()
print(f"remove leading: '{s2_remove_leading}'")

s2_remove_trailing = s2.rstrip()
print(f"remove trailing: '{s2_remove_trailing}'")

s2_remove_both = s2.strip()
print(f"remove both: '{s2_remove_both}'")

The output is:

Output
string: ' shark squid ' remove leading: 'shark squid ' remove trailing: ' shark squid' remove both: 'shark squid'

The output shows that using the strip methods with the chars argument omitted removes only the leading and trailing space, newline, and tab characters from the string. Any whitespace that’s not at the very beginning or end of the string isn’t removed.

Trimming a Specific Whitespace Character from a String Using Strip Methods

You can also remove only a character or characters from the beginning and end of a string by specifying the chars argument. The following example demonstrates how to trim only the leading newline character from a string:

s3 = '\n  sammy\n  shark\t  '
print(f"string: '{s3}'")

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

The output is:

Output
string: ' sammy shark ' remove only leading newline: ' sammy shark '

The output shows that the lstrip() method removes the leading newline character but doesn’t remove the leading spaces from the string.

Note that the strip method only removes specific characters when they’re the outermost leading and trailing characters. For example, you can’t use rstrip() to remove only the trailing tab character from s3 = '\n sammy\n shark\t ' because of the spaces after \t.

Conclusion

In this article, you used the strip(), rstrip(), and lstrip() methods to trim leading and trailing whitespace from strings. To learn how to remove spaces and characters from within strings, refer to How To Remove Spaces from a String In Python. Continue your learning with more Python string tutorials.

Thanks for learning with the DigitalOcean Community. Check out our offerings for compute, storage, networking, and managed databases.

Learn more about us


Want to learn more? Join the DigitalOcean Community!

Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.

Sign up now
About the authors
Default avatar
Pankaj

author

Still looking for an answer?

Ask a questionSearch for more help

Was this helpful?
 
JournalDev
DigitalOcean Employee
DigitalOcean Employee badge
May 22, 2020

Thank you very much for these examples. I was trying to understand the differences and your explanation was very easy to understand. I’ll check out more of your pages.

- Anna

    JournalDev
    DigitalOcean Employee
    DigitalOcean Employee badge
    April 30, 2020

    Is there a stock Python function that removed redundant spaces? For example: “Hello world” becomes “Hello Word”

    - SoCalDude