CARVIEW |
- Log in to:
- Community
- DigitalOcean
- Sign up for:
- Community
- DigitalOcean
By Pankaj Kumar

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. Whenchars
is omitted orNone
, returns a new string with all leading and trailing whitespace removed.rstrip([chars])
: Trims characters from the right side of a string. Whenchars
is omitted orNone
, returns a new string with all trailing whitespace removed.lstrip([chars])
: Trims characters from the left side of a string. Whenchars
is omitted orNone
, 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:
Outputstring: '
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:
Outputstring: '
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.
About the author
Java and Python Developer for 20+ years, Open Source Enthusiast, Founder of https://www.askpython.com/, https://www.linuxfordevices.com/, and JournalDev.com (acquired by DigitalOcean). Passionate about writing technical articles and sharing knowledge with others. Love Java, Python, Unix and related technologies. Follow my X @PankajWebDev
Still looking for an answer?
Is there a stock Python function that removed redundant spaces? For example: “Hello world” becomes “Hello Word”
- SoCalDude
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
- Table of contents
- Trimming Whitespace from a String Using Strip Methods
- Trimming a Specific Whitespace Character from a String Using Strip Methods
- Conclusion
Deploy on DigitalOcean
Click below to sign up for DigitalOcean's virtual machines, Databases, and AIML products.
Become a contributor for community
Get paid to write technical tutorials and select a tech-focused charity to receive a matching donation.
DigitalOcean Documentation
Full documentation for every DigitalOcean product.
Resources for startups and SMBs
The Wave has everything you need to know about building a business, from raising funding to marketing your product.
Get our newsletter
Stay up to date by signing up for DigitalOcean’s Infrastructure as a Newsletter.
New accounts only. By submitting your email you agree to our Privacy Policy
The developer cloud
Scale up as you grow — whether you're running one virtual machine or ten thousand.
Get started for free
Sign up and get $200 in credit for your first 60 days with DigitalOcean.*
*This promotional offer applies to new accounts only.