[Python] Find common characters between two strings

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

  1. Stack

    Stack Membro Participativo

    I am trying to print the common letters from two different user inputs. For example, if the inputs are "one" and "toe", the expected result is "eo".

    I need to do it using a for loop.

    I am running into two problems:


    1. My statement if char not in output... is not pulling unique values.


    2. The output is giving me a list of individual letters rather than a single string. I tried to split the output but split ran into a type error.

    wrd = 'one'
    sec_wrd = 'toe'

    def unique_letters(x):
    output =[]
    for char in x:
    if char not in output and char != " ":
    output.append(char)
    return output

    final_output = (unique_letters(wrd) + unique_letters(sec_wrd))

    print(sorted(final_output))


    How can I do it correctly?

    Continue reading...

Compartilhe esta Página