Easiest way to calculate number of character in string
Ove Svensson
svensson_ove at hotmail.com
Wed Dec 21 09:53:07 EST 2005
"P. Schmidt-Volkmar" <no_spam at tauth.de> writes:
> 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?
>
> Thanks,
>
> Pascal
>
>
What about this:
Zaehler += ';'*max(0,42-Zaehler.count(';'))
More information about the Python-list
mailing list