Python – How to trim a String?

In Python trim method is called strip, it removes the leading and trailing spaces of a String.


str.strip()  #trim
str.lstrip() #ltrim
str.rstrip() #rtrim

#!/usr/bin/python
str = "   abc   "

print(str)
print(str.strip())
print(str.lstrip())
print(str.rstrip())

Output


{space} abc {space}
abc
abc {space}
{space} abc

References

mkyong

Founder of Mkyong.com, passionate Java and open-source technologies. If you enjoy my tutorials, consider making a donation to these charities.

0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments