How do I count the number of spaces at the left end of a string?
Asun Friere
afriere at yahoo.co.uk
Thu May 17 02:18:21 EDT 2007
On May 17, 8:18 am, Steven Howe <howe.ste... at gmail.com> wrote:
> walterbyrd wrote:
> > I don't know exactly what the first non-space character is. I know the
> > first non-space character will be * or an alphanumeric character.
>
> using builtin function rindex
But only if there is a guarantee that are spaces nowhere else in the
string:
a = ' abc'
print a.rindex(' ') + 1
=> 4
Good, but ...
b = ' ab c'
b.rindex(' ') + 1
=> 7
Ouch!
The difference method suggested by Daniel Nogradi, seems the most
obvious way.
len(b) - len(b.lstrip())
=> 4
Asun
More information about the Python-list
mailing list