- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Common words among tuple strings in Python
When it is required to find common words among the tuple strings, the 'join' method, the 'set' method, the '&' operator and the 'split' method is used.
The 'join' method can be used to join multiple values based on a specific value,
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.
The 'split' function splits the given data into multiple sections depending on the value on which it needs to be split.
The '&' operator performs multiplication, i.e AND operation.
Below is a demonstration of the same −
my_tuple_1 = ('Hi there', 'Hi Will,', 'Hi ', 'Hi there') print("The tuple is : ") print(my_tuple_1) my_result = ", ".join(sorted(set(my_tuple_1.split()) & set(my_tuple_1.split()) & set(my_tuple_1.split()))) print("Common words among the tuples are : ") print(my_result)
The tuple is : ('Hi there', 'Hi Will,', 'Hi ', 'Hi there') Common words among the tuples are : Hi
- A tuple is defined and is displayed on the console.
- The tuples are split based on empty space, using the 'split' method.
- It is then sorted and joined using the 'sorted' and join' methods respectively.
- This is assigned to a value.
- It is displayed on the console.
- Common Words in Two Strings in Python
- Python program to remove words that are common in two Strings
- How to share common data among multiple Python files?
- Removing strings from tuple in Python
- Greatest Common Divisor of Strings in Python
- How can I convert Python strings into tuple?
- Python program to find uncommon words from two Strings
- Get first index values in tuple of strings in Python
- Count common characters in two strings in C++
- Count common subsequence in two strings in C++
- How can I create a Python tuple of Unicode strings?
- Python code to print common characters of two Strings in alphabetical order