Returning the lowest index for the first non whitespace character in a string in Python

Pablo picture Pablo · Mar 4, 2010 · Viewed 17.8k times · Source

What's the shortest way to do this in Python?

string = "   xyz"

must return index = 3

Answer

Frank picture Frank · Mar 4, 2010
>>> s = "   xyz"
>>> len(s) - len(s.lstrip())
3