Easiest way to calculate number of character in string

Kent Johnson kent at kentsjohnson.com
Wed Dec 21 09:30:09 EST 2005


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:
> 
> How can this be achieved easily?

Is this homework? str.count() and string multiplication are your friends here. See
http://docs.python.org/lib/string-methods.html
http://docs.python.org/lib/typesseq.html

Kent



More information about the Python-list mailing list