Home » Python » Python programs

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.
Submitted by Anuj Singh, on June 25, 2019

In this article, we are going to implement some of the features of if statement to verify the characters present inside the entered 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.

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





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.
Learn PCB Designing: PCB DESIGNING TUTORIAL




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing » Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates

© https://www.includehelp.com some rights reserved.