By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,846 Members | 1,872 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,846 IT Pros & Developers. It's quick & easy.

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

P: n/a
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.

May 17 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
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()

--
Kind Regards,
Anthony Irwin

http://www.irwinresources.com
http://www.makehomebusiness.com
email: anthony at above domains, - www.
May 17 '07 #2

P: n/a
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.
You really need to stop posting the same message multiple times.

A possible answer using regular expressions:
>>import re
whitespace_matcher = re.compile(r'^\s+')
match = whitespace_matcher.search(' abc def ghi')
len(match.group())
8

STeVe
May 17 '07 #3

P: n/a
On 2007-05-16 20:02:18 -0600, walterbyrd <wa********@iname.comsaid:
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())

May 17 '07 #4

P: n/a
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.
May 17 '07 #5

P: n/a
On May 16, 9:02 pm, walterbyrd <walterb...@iname.comwrote:
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

May 17 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.