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 −

 Live Demo

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.

Updated on: 12-Mar-2021

333 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements