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" 

One thought on “How to trim a string in Python”

Leave a Reply