how do I count spaces at the beginning of a string?

Anthony Irwin nospam at noemailhere.nowhere
Wed May 16 23:31:41 EDT 2007


Anthony Irwin wrote:
> walterbyrd wrote:
>> The strings start with whitespace, and have a '*' or an alphanumeric
>> character. I need to know how many whitespace characters exist at the
>> beginning of the string.
>>
> 
> Hi,
> 
> I am new to python and just really learning but this is what I came up 
> with.
> 
> #!/usr/bin/env python
> 
> def main():
>     s = "      abc def ghi"
>     count = 0
> 
>     for i in s:
>         if i == ' ':
>             count += 1
>         else:
>             break
> 
>     print count
> 
> if __name__ == '__main__':
>     main()
> 

Ahh even better would be to use some of the python library functions 
that I am still finding more about.

s = "      abc def ghi"
print (len(s) - len(s.lstrip()))

I am really starting to like python the more I use it.

-- 
Kind Regards,
Anthony Irwin

http://www.irwinresources.com
http://www.makehomebusiness.com
email: anthony at above domains, - www.



More information about the Python-list mailing list