Python Program to Print All Letters Present in Both Strings

This is a Python Program to display which letters are present in both the strings.

Problem Description

The program takes two strings and displays which letters are present in both the strings.

Problem Solution

1. Enter two input strings and store it in separate variables.
2. Convert both of the strings into sets and find the union between both the sets.
3. Store the letters in a list.
4. Use a for loop to print the letters of the list.
5. Exit.

Program/Source Code

Here is source code of the Python Program to display which letters are present in both the strings. The program output is also shown below.

s1=raw_input("Enter first string:")
s2=raw_input("Enter second string:")
a=list(set(s1)|set(s2))
print("The letters are:")
for i in a:
    print(i)
Program Explanation

1. User must enter two input strings and store it in separate variables.
2. Both of the strings are converted into sets and the union of both the sets are found using the ‘|’ operator.
3. These letters are stored in a list.
4. A for loop is used to print the letters of the list.

advertisement
advertisement
Runtime Test Cases
 
Case 1:
Enter first string:hello
Enter second string:world
The letters are:
e
d
h
l
o
r
w
 
Case 2:
Enter first string:test
Enter second string:string
The letters are:
e
g
i
n
s
r
t

Sanfoundry Global Education & Learning Series – Python Programs.

To practice all Python programs, here is complete set of 150+ Python Problems and Solutions.

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.