How to remove leading and trailing spaces from a string?

You can use the strip() method to remove trailing and leading spaces:

>>> s="   abd cde   "
>>> s.strip()
'abd cde'

Note: the internal spaces are preserved.

Leave a Comment