Check if two Strings have the same Characters in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Check if two Strings have the same Characters in Python
  2. Check if two strings have the same characters using a for loop
  3. Check if two Strings have the same Characters using collections.Counter

# Check if two Strings have the same Characters in Python

To check if two strings have the same characters:

  1. Use the sorted() function to sort the two strings.
  2. Use the equality operator to compare the results.
  3. If the comparison evaluates to True, the two strings have the same characters.
main.py
def have_same_characters(str1, str2): return sorted(str1) == sorted(str2) print(have_same_characters('bob', 'obb')) # ๐Ÿ‘‰๏ธ True print(have_same_characters('bob', 'obbb')) # ๐Ÿ‘‰๏ธ False print(have_same_characters('bob', 'abc')) # ๐Ÿ‘‰๏ธ False

check if two strings have the same characters

The code for this article is available on GitHub

The sorted() function takes an iterable and returns a new sorted list from the items in the iterable.

main.py
print(sorted('bob')) # ๐Ÿ‘‰๏ธ ['b', 'b', 'o'] print(sorted('obb')) # ๐Ÿ‘‰๏ธ ['b', 'b', 'o']
If the two strings have the same length and contain the same characters the comparison evaluates to True.

We sort the characters before comparing, so the order of the characters in the strings is not important.

The function from the code sample returns True only if the two strings contain the same characters and have the same length.

# Check if two strings have the same characters using a for loop

If you need to check if two strings have the same characters, not considering the length of the strings and the frequency of each character, use a for loop.

main.py
def have_same_characters(str1, str2): for char in str1: if not char in str2: return False return True print(have_same_characters('bob', 'obb')) # ๐Ÿ‘‰๏ธ True print(have_same_characters('bob', 'obbbb')) # ๐Ÿ‘‰๏ธ True print(have_same_characters('abc', 'aabbcc')) # ๐Ÿ‘‰๏ธ True print(have_same_characters('bob', 'abc')) # ๐Ÿ‘‰๏ธ False

check if two strings have same characters using for loop

The code for this article is available on GitHub

We used a for loop to iterate over one of the strings.

On each iteration, we check if the current character is contained in the other string.

If the condition is not met, we return False from the function right away.

If the condition is met for all characters in the string, the function returns True.

This approach only checks if all characters in string A are contained in string B.

The two strings don't have to have the same length for the condition to be met.

# Check if two Strings have the same Characters using collections.Counter

You can also use the collections.Counter class to check if two strings have the same characters.

main.py
from collections import Counter str_1 = 'bob' str_2 = 'obb' if Counter(str_1) == Counter(str_2): # ๐Ÿ‘‡๏ธ this runs print('The strings have the same characters') else: print('The strings do NOT have the same characters')

check if two strings have the same characters using counter

The code for this article is available on GitHub

The Counter class from the collections module is a subclass of the dict class.

The class is basically a mapping of key-count pairs.

main.py
from collections import Counter str_1 = 'bob' str_2 = 'obb' print(Counter(str_1)) # Counter({'b': 2, 'o': 1}) print(Counter(str_2)) # Counter({'b': 2, 'o': 1})

If the Counter objects contain the same key-count pairs, the two strings contain the same characters.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev