Q:

Python program to find the matched characters in a given string

belongs to collection: Python String Programs

0

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.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Python String Programs

This question belongs to these collections

Similar questions


need a help?


find thousands of online teachers now
Python | Find the frequency of each character in a... >>
<< Python program to check whether a string contains ...