pythonstringstring-matching

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


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

string = "   xyz"

must return index = 3


Solution

  • >>> s = "   xyz"
    >>> len(s) - len(s.lstrip())
    3