Is there a Python function that will trim whitespace (spaces and tabs) from a string?
So that given input " \t example string\t " becomes "example string".
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
For whitespace on both sides, use
str.strip:For whitespace on the right side, use
str.rstrip:For whitespace on the left side, use
str.lstrip:You can provide an argument to strip arbitrary characters to any of these functions, like this:
This will strip any space,
\t,\n, or\rcharacters from both sides of the string.The examples above only remove strings from the left-hand and right-hand sides of strings. If you want to also remove characters from the middle of a string, try
re.sub:That should print out: