# Counting how many chars equal to a given char are in the beginning of a string

Discussion in 'Python' started by Stormbringer, Dec 22, 2003.

1. ### StormbringerGuest

Hi,

Given a string s and a char c, is there a short & elegant way to
determine how many consecutive occurences of c are in the beginning of
s ?

For example if s = ">>>>message1" and c = ">" then the number I am
looking for is 4 (the string begins with 4 '>').

Thanks,
Andrei

Stormbringer, Dec 22, 2003

2. ### Jeff EplerGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

Here are several ways:

def count_initial(s, c):
return len(s) - len(s.lstrip(c))

def count_initial(s, c):
r = "^" + re.escape(c) + "*"
m = re.match(r, s)
return len(m.group(0))

# enumerate in 2.3
def count_initial(s, c):
for i, j in enumerate(s):
if j != c: break
return i

# itertools in 2.3
def count_initial(s, c):
return len(list(itertools.takewhile(lambda x: x==c, s)))

Jeff

Jeff Epler, Dec 22, 2003

3. ### Skip MontanaroGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

Andrei> Given a string s and a char c, is there a short & elegant way to
Andrei> determine how many consecutive occurences of c are in the
Andrei> beginning of s ?

Andrei> For example if s = ">>>>message1" and c = ">" then the number I
Andrei> am looking for is 4 (the string begins with 4 '>').

def howmanyatstart(s, pfx):
return (len(s) - len(s.lstrip(pfx)))/len(pfx)

? This works for prefixes which are longer than a single character:

>>> howmanyatstart(">>>>message1", '>')

4
>>> howmanyatstart("bobbobbob>>>>message1", 'bob')

3

Skip

Skip Montanaro, Dec 22, 2003
4. ### vincent wehrenGuest

"Stormbringer" <> schrieb im Newsbeitrag
news:...
| Hi,
|
| Given a string s and a char c, is there a short & elegant way to
| determine how many consecutive occurences of c are in the beginning of
| s ?
|
| For example if s = ">>>>message1" and c = ">" then the number I am
| looking for is 4 (the string begins with 4 '>').

def getc(s, c):
"""
Get the number of consecutive occurrences of given char c
at the beginning of given string s.
"""
cnt = 0
while 1:
try:
if s[cnt] == c:
cnt += 1
else:
return cnt
except IndexError:
return cnt

c = '>'
print getc(">>>>Message", c) #prints 4
print getc(" >>>>Message", c) #prints 0
print getc (">>>", c) #prints 3

Maybe not the most elegant or shortest, but it works...

HTH,

Vincent Wehren

|
| Thanks,
| Andrei

vincent wehren, Dec 22, 2003
5. ### vincent wehrenGuest

"vincent wehren" <> schrieb im Newsbeitrag
news:bs7nrc\$ubp\$1.nb.home.nl...
| "Stormbringer" <> schrieb im Newsbeitrag
| news:...
| | Hi,
| |
| | Given a string s and a char c, is there a short & elegant way to
| | determine how many consecutive occurences of c are in the beginning of
| | s ?
| |
| | For example if s = ">>>>message1" and c = ">" then the number I am
| | looking for is 4 (the string begins with 4 '>').
|
|
| def getc(s, c):
| """
| Get the number of consecutive occurrences of given char c
| at the beginning of given string s.
| """
| cnt = 0
| while 1:
| try:
| if s[cnt] == c:
| cnt += 1
| else:
| return cnt
| except IndexError:
| return cnt
|
| c = '>'
| print getc(">>>>Message", c) #prints 4
| print getc(" >>>>Message", c) #prints 0
| print getc (">>>", c) #prints 3
|
| Maybe not the most elegant or shortest, but it works...

Looking at Jeff's proposed solutions, I would like to replace
the "Maybe" part in the above sentence with "Definitively"...

Isn't Python great?

Vincent

Vincent Wehren

|
| HTH,
|
| Vincent Wehren
|
|
| |
| | Thanks,
| | Andrei
|
|

vincent wehren, Dec 22, 2003
6. ### Jeff EplerGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
>
> def howmanyatstart(s, pfx):
> return (len(s) - len(s.lstrip(pfx)))/len(pfx)
>
> ? This works for prefixes which are longer than a single character:
>
> >>> howmanyatstart(">>>>message1", '>')

> 4
> >>> howmanyatstart("bobbobbob>>>>message1", 'bob')

> 3

strip() doesn't work that way:
>>> "bbbbbbbxxx".strip("bob")

'xxx'

Jeff Epler, Dec 22, 2003
7. ### Skip MontanaroGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

>>>>> "Jeff" == Jeff Epler <> writes:

Jeff> On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
>>
>> def howmanyatstart(s, pfx):
>> return (len(s) - len(s.lstrip(pfx)))/len(pfx)
>>
>> ? This works for prefixes which are longer than a single character:
>>
>> >>> howmanyatstart(">>>>message1", '>')

>> 4
>> >>> howmanyatstart("bobbobbob>>>>message1", 'bob')

>> 3

Jeff> strip() doesn't work that way:
>>>> "bbbbbbbxxx".strip("bob")

Jeff> 'xxx'

Then it looks like a bug in one or the other to me.

Skip

Skip Montanaro, Dec 22, 2003
8. ### Jeff EplerGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

On Mon, Dec 22, 2003 at 04:06:44PM -0600, Skip Montanaro wrote:
> Then it looks like a bug in one or the other to me.

Add 'assert len(c) == 1' at the top of mine, then.

Jeff

Jeff Epler, Dec 22, 2003
9. ### Skip MontanaroGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

>>>>> "Jeff" == Jeff Epler <> writes:

Jeff> On Mon, Dec 22, 2003 at 03:16:41PM -0600, Skip Montanaro wrote:
>>
>> def howmanyatstart(s, pfx):
>> return (len(s) - len(s.lstrip(pfx)))/len(pfx)
>>
>> ?

Jeff> strip() doesn't work that way:
>>>> "bbbbbbbxxx".strip("bob")

Jeff> 'xxx'

Skip> Then it looks like a bug in one or the other to me.

I retract my statement. It's a bug in my code. help("".lstrip) shows why:

lstrip(...)
S.lstrip([chars]) -> string or unicode

Return a copy of the string S with leading whitespace removed.
If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping

Note the second sentence.

Sorry for the flub.

Skip

Skip Montanaro, Dec 22, 2003
10. ### Oren TiroshGuest

Re: Counting how many chars equal to a given char are in thebeginning of a string

On Mon, Dec 22, 2003 at 12:04:21PM -0800, Stormbringer wrote:
> Hi,
>
> Given a string s and a char c, is there a short & elegant way to
> determine how many consecutive occurences of c are in the beginning of
> s ?
>
> For example if s = ">>>>message1" and c = ">" then the number I am
> looking for is 4 (the string begins with 4 '>').

re.match('>*', s).end()

Oren

Oren Tirosh, Dec 23, 2003
11. ### Bengt RichterGuest

On 22 Dec 2003 12:04:21 -0800, (Stormbringer) wrote:

>Hi,
>
>Given a string s and a char c, is there a short & elegant way to
>determine how many consecutive occurences of c are in the beginning of
>s ?
>
>For example if s = ">>>>message1" and c = ">" then the number I am
>looking for is 4 (the string begins with 4 '>').
>

Not tested beyond this example ;-)

>>> def countleading(c, s): return len(s)-len(s.lstrip(c))

...
>>> s = ">>>>message1"

4

Regards,
Bengt Richter

Bengt Richter, Dec 23, 2003
12. ### Bengt RichterGuest

On 23 Dec 2003 12:57:47 GMT, (Bengt Richter) wrote:

>On 22 Dec 2003 12:04:21 -0800, (Stormbringer) wrote:
>
>>Hi,
>>
>>Given a string s and a char c, is there a short & elegant way to
>>determine how many consecutive occurences of c are in the beginning of
>>s ?
>>
>>For example if s = ">>>>message1" and c = ">" then the number I am
>>looking for is 4 (the string begins with 4 '>').
>>

>Not tested beyond this example ;-)
>
> >>> def countleading(c, s): return len(s)-len(s.lstrip(c))

> ...
> >>> s = ">>>>message1"

> 4
>

I wouldn't have posted this if the Re: replies had been in the same thread