473,480 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

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
5 12901
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
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
11168
by: Marc Durufle | last post by:
I have a string like that " Vertices " and i want to obtain "Vertices" I would want to know if there is a simple way to put off spaces on a string, thank you -- Marc Durufle Inria...
5
4051
by: Marco Gallo | last post by:
I've been trying to get rid of extra spaces in a table that I created with addresses in it. For instance in a field called TEST, I got the following address: " 1 main st Apt A "...
3
2387
by: romayankin | last post by:
I'm trying to create PHP trim() function equivalent using pure JS code that truncates spaces from the begging and from the end of the given string. One of the solutions is to use two "while" loops...
5
3579
by: Andrew Wingorodov | last post by:
im made subj like this: inline bool isnt_space (int sp) { return (::isspace (sp)) ? false : true; } str.erase ( std.begin () , std::find_if ( str.begin(), str.end(), isnt_space) );
0
6904
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6873
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5321
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4767
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2990
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.