strip() String Method in Python with Examples

strip() method removes all white spaces at the begining and end of the string.

Example: strip() String Method


message = '    welcome to python 123!    '
print('Original string: %s' % (message))

print('After strip: %s' % (message.strip()) ) 

# OR
print('After strip: %s' % (str.strip(message)) ) 

Output

Original string:     welcome to python 123!    
After strip: welcome to python 123!

Note: if it is required to remove all white spaces from begining of the string then we can use lstrip() method like strip().

Note: if it is required to remove all white spaces from end of the string then we can use lstrip() method like strip().



Related String Methods