print re.sub('[\s+]', '', s)
s = " \t a string example\t "
s = s.strip()
s = s.lstrip()
s = s.rstrip()
s = s.strip(' \t\n\r')
As thedz points out, you can provide an argument to strip arbitrary characters to any of these functions like this:
Results for the examples should be quite helpful :)
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:
This will strip any space, \t, \n, or \r characters from the left-hand side, right-hand side, or both sides of the string.
Whitespace on the left side:
Whitespace on the right side:
str is a type, so you should avoid using it as a variable name..
strip() takes in an arguemnt to tell it what to trip. Try: strip(' \t\n\r')
this function does not seem to strip preceding tabs..