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

W

walterbyrd

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.
 
A

Anthony Irwin

walterbyrd said:
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()

--
Kind Regards,
Anthony Irwin

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

Steven Bethard

walterbyrd said:
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.

You really need to stop posting the same message multiple times.

A possible answer using regular expressions:
8

STeVe
 
N

Norman Lorrain

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.
a = ' three spaces'
print len(a) -len(a.lstrip())
 
A

Anthony Irwin

Anthony said:
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.
 
P

Paul McGuire

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.

.....aaaaaaand what have you tried so far?

This really is a pretty basic question - to most folks on this list,
it's about the same as "how do I count the fingers on my hand?". Is
this your first Python program? Homework assignment? First program
ever written in any language? Try reading one of the online tutorials
or pick up a Python book or browse the archive from this list or the
python-tutor mailing list - you shouldn't have to read too far to
start getting an idea on how to approach this problem. www.python.org
has links to all of these and more.

Take a stab at this problem, and if you don't have any luck, come back
(or try the python-tutor list) and post what you've tried.

-- Paul
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top