How to get intersection of two strings in Python

2 Answers

0 votes
string1 = "php"
string2 = "python"

intersection = set(string1) & set(string2)

print("Intersection:", ''.join(intersection))

     

'''
run:

Intersection: ph

'''

 



answered Jul 6, 2025 by avibootz
0 votes
string1 = "php"
string2 = "python"

intersection = set(string1).intersection(string2)

print("Intersection:", ''.join(intersection))

     

'''
run:

Intersection: ph

'''

 



answered Jul 6, 2025 by avibootz
...