1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.
  2. Anuncie Aqui
    Anuncie aqui você Também: fdantas@4each.com.br

[Python] Python Count Characters

Discussão em 'Python' iniciado por Stack, Outubro 2, 2024.

  1. Stack

    Stack Membro Participativo

    Write a program whose input is a string which contains a character and a phrase, and whose output indicates the number of times the character appears in the phrase. The output should include the input character and use the plural form, n's if the number of times the characters appears is not exactly 1. Ex: If the input is:

    n Monday the output is:

    1 n Ex: If the input is:

    z Today is Monday the output is:

    0 z's Ex: If the input is:

    n It's a sunny day the output is:

    2 n's Case matters. n is different than N.

    Ex: If the input is:

    n Nobody the output is:

    0 n's

    This is what I have so far:

    user_string=input(str())
    character=user_string[0]
    phrase=user_string[1]
    count=0

    for i in phrase:
    if i == character:
    count = count+1

    if count!= 1:
    print(str(count) + " " + character + "'s")
    else:
    print(str(count) + " " + character)


    This works great for the phrases that have 0 characters matching. But its not counting the ones that should match.

    Continue reading...

Compartilhe esta Página