Fastest way to calculate leading whitespace
Wolfram Hinderer
wolfram.hinderer at googlemail.com
Sat May 8 15:15:22 EDT 2010
On 8 Mai, 20:46, Steven D'Aprano <st... at REMOVE-THIS-
cybersource.com.au> wrote:
> def get_leading_whitespace(s):
> t = s.lstrip()
> return s[:len(s)-len(t)]
>
> >>> c = get_leading_whitespace(a)
> >>> assert c == leading_whitespace
>
> Unless your strings are very large, this is likely to be faster than any
> other pure-Python solution you can come up with.
Returning s[:-1 - len(t)] is faster.
More information about the Python-list
mailing list