In this article, we are going to implement some of the features of if statement to verify the characters present inside the entered string.
在本文中,我们将实现if语句的某些功能,以验证输入的string中存在的字符 。
User is asked to enter a string with many characters as per user's choice and the code will check whether the string has some of the listed characters inside in it. If the string has all characters matched with the listed one, then it prints "Completely In" and else "Not In" with the number of characters matched.
根据用户的选择,要求用户输入包含许多字符的字符串,然后代码将检查该字符串是否包含其中列出的某些字符 。 如果该字符串的所有字符都与列出的字符匹配,则它会打印“ Completely In” ,否则显示“ Not In”,并显示匹配的字符数。
So here is the code:
所以这是代码:
secretword = str(input("Enter a string: "))
lettersGuessed = ['a', 'e', 'i', 'k', 'p', 'r', 's','l']
flag = 0
for i in secretword:
if i in lettersGuessed:
flag+=1
if len(secretword) == flag:
print("Completely In")
else:
print("Not In")
print(flag, 'matches')
Output:
输出:
First run:
Enter a string: aeiprsl
Completely In
7 matches
Second run:
Enter a string: xyz
Not In
0 matches
翻译自: https://www.includehelp.com/python/find-the-matched-characters-in-a-given-string.aspx