Easiest way to calculate number of character in string
Carsten Haese
carsten at uniqsys.com
Wed Dec 21 09:25:46 EST 2005
On Wed, 2005-12-21 at 09:03, P. Schmidt-Volkmar wrote:
> 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
AnzahlSemikolon = Zeile.count(';')
if AnzahlSemikolon < 42:
Zeile += ';'*(42-AnzahlSemikolon)
HTH,
Carsten.
More information about the Python-list
mailing list