Easiest way to calculate number of character in string

Lawrence Oluyede raims at dot.com
Wed Dec 21 09:26:05 EST 2005


Il 2005-12-21, P. Schmidt-Volkmar <no_spam at tauth.de> ha scritto:
> Hi there,
>
> I have a string in which I want to calculate how often the character  ';' 
> occurs. If the character does not occur 42 times, the ";" should be added so 
> the 42 are reached.
>
> My solution is slow and wrong:
>     for Position in range (0, len(Zeile)):
>         if Zeile[Position]==';': AnzahlSemikolon = AnzahlSemikolon +1
>     if AnzahlSemikolon < 42:
>         for Zaehler in range(AnzahlSemikolon, 42):
>             Zeile = Zeile + ';'
>         Dreckskram = Dreckskram +1
>
> How can this be achieved easily?

One way can be this I think:

s = "your_string_full_of_;;;;;"
num_of_semicolons = len(s.split(";") - 1)



-- 
Lawrence - http://www.oluyede.org/blog
"Anyone can freely use whatever he wants but the light at the end
of the tunnel for most of his problems is Python"



More information about the Python-list mailing list