
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python - Intersection of two String
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.
- Initialize two strings and an empty string.
- Iterate over the first string and add the current character to the new string if it presents in the second string as well and not present in new string already.
- Print the result.
Example
# 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.
Output
tu
We'll use the set to intersect two strings. Follow the below steps.
- Convert two strings into sets.
- Intersect two sets using intersection method.
- Convert the result to string using join method.
- Print the result.
Example
# 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.
Output
ut
Conclusion
If you have any queries in the article, mention them in the comment section.
- Related Articles
- Intersection of Two Arrays II in Python
- Intersection of Two Linked Lists in Python
- Python program to find Intersection of two lists?
- Python Pandas - Form the intersection of two Index objects
- Intersection of two arrays JavaScript
- Intersection of two arrays in Java
- Intersection of two arrays in C#
- Intersection of two HashSets in C#
- Intersection of Two Arrays in C++
- Python - Fetch columns between two Pandas DataFrames by Intersection
- Python - Intersection of multiple lists
- The intersection of two arrays in Python (Lambda expression and filter function )
- Intersection of Two Linked Lists in C++
- Explain the intersection process of two DFA’s
- Intersection() function Python

Advertisements