Python program to find the matched characters in a given string

Here, we are going to implement a python program in which we have to compare the strings and find the matched characters with given string and user entered string. By Anuj Singh Last updated : February 25, 2024

In this article, we are going to implement some of the features of if statement to verify the characters present inside the entered string.

Problem statement

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.

So here is the code:

Code to find the matched characters in a given string in Python

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

To understand the above program, you should have the basic knowledge of the following Python topics:

Python String Programs »

Related Programs

Comments and Discussions!

Load comments ↻





Copyright © 2024 www.includehelp.com. All rights reserved.