In this article, we are going to learn how to intersect two strings in different ways.
Follow the below the steps to solve the problem.
# initializing the string string_1 = 'tutorialspoint' string_2 = 'tut' result = '' # finding the common chars from both strings for char in string_1: if char in string_2 and not char in result: result += char # printing the result print(result)
If you run the above code, then you will get the following result.
tu
We'll use the set to intersect two strings. Follow the below steps.
# initializing the string string_1 = 'tutorialspoint' string_2 = 'tut' # intersection result = set(string_1).intersection(string_2) # converting to string result_str = ''.join(result) # printing the result print(result_str)
If you run the above code, then you will get the following result.
ut
If you have any queries in the article, mention them in the comment section.