Remove characters from both sides of a string in Python
Strip from both side of a string
The strip method trims the whitespace from both sides of a string. We can also specify what characters to trim.
strip method has the following syntax.
s.strip([chars])
Removes leading and trailing whitespace from string s (or characters in chars if passed).
print ' java2s.com '.strip()
The code above generates the following result.
As with lower, strip can be useful when comparing input to stored values.
names = ['Python', 'Java', 'C#']
name = 'Python '
if name in names: print 'Found it!'
if name.strip() in names: print 'Found it!'
# from w ww. j a v a 2 s . c o m
print '*** Java2s.com * for * everyone!!! ***'.strip(' *!')
The code above generates the following result.