- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program that Displays which Letters are in the First String but not in the Second
When it is required to display the letters that are present in the first string but not in the second string, two string inputs are taken from user. The ‘set’ is used to find the difference between the two strings.
Python comes with a datatype known as ‘set’. This ‘set’ contains elements that are unique only.
The set is useful in performing operations such as intersection, difference, union and symmetric difference.
Example
Below is a demonstration for the same −
my_str_1 = input("Enter the first string...") my_str_2 = input("Enter the second string...") my_result = list(set(my_str_1)-set(my_str_2)) print("The letters in first string but not in second string :") for i in my_result: print(i)
Output
Enter the first string...Jane Enter the second string...Wane The letters in first string but not in second string : J
Explanation
- Two strings are taken as input from the user.
- They are converted to a set, and their difference is computed.
- This difference is converted to a list.
- This value is assigned to a variable.
- This is iterated over, and displayed on the console.
- Related Articles
- Python Program that Displays which Letters are in the Two Strings but not in Both
- Python Program that Displays which Letters are Present in Both the Strings
- Delete elements in first string which are not in second string in JavaScript
- Find the character in first string that is present at minimum index in second string in Python
- Count elements present in first array but not in second in C++
- Program to find all words which share same first letters in Python
- Find elements which are present in first array and not in second in C++
- Python program to calculate the number of digits and letters in a string
- How to concatenate two strings so that the second string must concatenate at the end of first string in JavaScript?
- How to concatenate two strings so that the second string must concatenate at the end of the first string in JavaScript?
- Check whether second string can be formed from characters of first string in Python
- Get first three letters from every string in C#
- Interchanging first letters of words in a string in JavaScript
- Is the second string a rotated version of the first string JavaScript
- Program to check the string is repeating string or not in Python

Advertisements