Skip to content Skip to sidebar Skip to footer

Regular Expression - Python - Remove Leading Whitespace

I search a text file for the word Offering with a regular expression. I then use the start and end points from that search to look down the column and pull the integers. Some insta

Solution 1:

You could use strip() to remove leading and trailing whitespaces:

In [1]: ' 56.00  '.strip()
Out[1]: '56.00'

Solution 2:

'^\s+|\s+$'

Use this to regular expression access leading and trailing whitespaces.

Solution 3:

If you want to remove only the leading white spaces using regular expressions, you can use re.sub to do that.

>>> import re
>>>re.sub(r"^\s+" , "" , "  56.45")
'56.45'

Post a Comment for "Regular Expression - Python - Remove Leading Whitespace"