[Python] How can I check whether a character is at least twice inside a string?

Discussão em 'Python' iniciado por Stack, Fevereiro 8, 2025.

  1. Stack

    Stack Membro Participativo

    String string_data is read from input. If "r" is in string_data, then:

    Output "At: " followed by the index of the first occurrence. Create a string from string_data with the first two occurrences of "r" replaced by "(deleted)" and output the new string on a new line.

    Otherwise, output "No matches".

    can anybody crack this one I know I may need an elif statement somewhere also need a else statement at the end for the otherwise part of the question.

    I tried this code it works up until the else: statement or the otherwise, output "No Matches".

    string_data = input()

    if 'r' in string_data:
    index = string_data.find('r')
    print(f'At:', index)

    new_str = string_data.replace("r", "(deleted)", 2)

    print(new_str, end='')
    print()

    Continue reading...

Compartilhe esta Página