编码挑战字符串方法Python

时间:2018-09-15 20:17:08

标签: python string methods

代码:

# function receives user input
def ask_sentence():
    # .replace() function takes two parameters (old string, new string replacement)
    sentence1 = input("Enter sentence 1: ").lower().replace(" ","") 
    sentence2 = input("Enter sentence 2: ").lower().replace(" ","") 

ask_sentence()

帮助:

我正在尝试创建一个程序,该程序将两个句子作为用户输入,然后仅将两个句子中出现的字母输出一次

例如:

sentence1 ='我喜欢奶酪'

sentence2 ='我需要鞋子'

输出=网格

1 个答案:

答案 0 :(得分:0)

为此,我认为set是完美的:

# function receives user input
def ask_sentence():
    # .replace() function takes two parameters (old string, new string replacement)
    sentence1 = set(input("Enter sentence 1: ").lower().replace(" ",""))
    sentence2 = set(input("Enter sentence 2: ").lower().replace(" ",""))

    print(sentence1)
    print(sentence2)

ask_sentence()