To find the characters in the first string that are not present in the second string in Python, you can use sets and list comprehension.
In this example,
str1
and str2
with different values.str1
set that are not in str2
set.# Find Characters Not Present in Second String
str1 = 'Hello World'
str2 = 'apple'
set1 = set(str1)
set2 = set(str2)
diff = ''.join(char for char in set1 if char not in set2)
print('Characters not present in second string:', diff)
Characters not present in second string: r WodH
In this tutorial, we learned How to find the Characters in the First String that are not Present in the Second String in Python language with well detailed examples.