Purpose
Get a string that contains no extra spaces
Method
You can use the following methods of string processing:
String.lstrip (s[, chars])
Return a copy of the string with leading characters removed. If chars is omitted or None, the whitespace characters is removed. If given and not None, chars must is a string; The characters in the string would be stripped from the beginning of the string this method is called on.
String.rstrip (s[, chars])
Return a copy of the string with trailing characters removed. If chars is omitted or None, the whitespace characters is removed. If given and not None, chars must is a string; The characters in the string would be stripped from the end of the "This" method is called on.
String.strip (s[, chars])
Return a copy of the string with leading and trailing characters removed. If chars is omitted or None, the whitespace characters is removed. If given and not None, chars must is a string; The characters in the string would be stripped from the both ends of the string this method is called on.
The specific results are as follows:
The code is as follows:
In [ten]: x= ' hi,jack! '
In [all]: print ' | ', X.lstrip (), ' | ', X.rstrip (), ' | ', X.strip (), ' | '
| hi,jack! | hi,jack! | hi,jack! |
The parameters provided by chars are used to remove specific symbols, noting that the spaces are not removed, for example:
The code is as follows:
In []: x= ' yxyxyxxxyy Hello xyxyxyy '
In []: Print X.strip (' xy ')
Hello