How to trim a string in Python

To trim a string in Python you can use the following snippet.

Sample Python

input = "   HelloWorld   "

input.strip()            # returns "HelloWorld"  
input.lstrip()           # returns "HelloWorld   "  
input.rstrip()           # returns "   HelloWorld"  
input.replace(' ', '')   # returns "HelloWorld" 

Please Share us:
Please rate this snippet

One thought on “How to trim a string in Python

Leave a Reply