Search code examples
pythonlistcs50

CS50P PSET2: Setting up my twttr: How to union two string


vowel = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]

vowel_remove = input("Input: ")
print("Output: ", end="")

for vowels in vowel:
    lower_vowel = vowels.lower()
    # print(lower_vowel)
    if vowels in vowel_remove:
        print(vowel_remove.replace(vowels, ""))

print()
Input: Apple
Output: Appl
pple

I get two output with each one of the vowel is removed one by one and making two output.

Is there a way to union the different string like seeing the similarity of the characters and removing the odd one like for example

Appl and pple the similar ones are ppl and the odd one from each of them are A and e

is it possible to union ppl


Solution

  • The below code iterates the input and removes the vowels and save the non vowels to an output variable.

    vowel = ["A", "E", "I", "O", "U", "a", "e", "i", "o", "u"]
    vowel_remove = input("Input: ")
    print("Output: ", end="")
    
    output = ""
    for char in vowel_remove:
        if char in vowel:
            continue
        else:
            output += char
    print(output)
    

    output

    Input: Apple
    Output: ppl
    

    To find unique characters in 2 strings you can use

    print(''.join(set('Appl').intersection('pple')))
    

    But this will remove repeating characters for example, 'p' in the above case.

    output

    lp