Check if a character appears twice in a String in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
3 min

banner

# Table of Contents

  1. Check if a character appears twice in a String in Python
  2. Check if a string has repeated characters in Python
  3. Find the duplicate characters in a String

# Check if a character appears twice in a String in Python

Use the str.count() method to check if a character appears twice in a string, e.g. if my_str.count(char) == 2:.

The str.count() method returns the number of occurrences of a substring in a string.

main.py
my_str = 'bobbyhadz.com' if my_str.count('o') == 2: # ๐Ÿ‘‡๏ธ this runs print('The character appears twice in the string') else: print('The character does NOT appear twice in the string') print(my_str.count('o')) # ๐Ÿ‘‰๏ธ 2 print(my_str.count('.')) # ๐Ÿ‘‰๏ธ 1 print(my_str.count('b')) # ๐Ÿ‘‰๏ธ 3

check if character appears twice in string

The code for this article is available on GitHub

The str.count() method returns the number of occurrences of a substring in a string.

main.py
my_str = 'bobbyhadz.com' if my_str.count('o') == 2: # ๐Ÿ‘‡๏ธ this runs print('The character appears twice in the string') else: print('The character does NOT appear twice in the string') print(my_str.count('o')) # ๐Ÿ‘‰๏ธ 2 print(my_str.count('.')) # ๐Ÿ‘‰๏ธ 1 print(my_str.count('b')) # ๐Ÿ‘‰๏ธ 3 print(my_str.count('X')) # ๐Ÿ‘‰๏ธ 0

If the method returns 2, then the character appears twice in the string.

# Check if a character appears more than once in a String

You can use the same approach to check if a character appears more than once in a string.

main.py
my_str = 'bobbyhadz.com' if my_str.count('b') > 1: # ๐Ÿ‘‡๏ธ this runs print('The character appears more than once in the string') else: print('The character does NOT appear more than once')

check if character appears more than once in string

The code for this article is available on GitHub

The character appears more than once in the string, so the if block runs.

# Check if a string has repeated characters in Python

If you need to check if the string has repeated characters, convert the string to a set and compare the length of the set to the string's length.

main.py
my_str = 'bobby' has_repeated_chars = len(set(my_str)) != len(my_str) print(has_repeated_chars) # ๐Ÿ‘‰๏ธ True my_str = 'abc' has_repeated_chars = len(set(my_str)) != len(my_str) print(has_repeated_chars) # ๐Ÿ‘‰๏ธ False

check if string has repeated characters

The code for this article is available on GitHub

We used the set() class to convert the string to a set object.

Set objects are an unordered collection of unique elements, so any duplicate characters get removed when converting to a set.

If the length of the set is not equal to the string's length, then the string has repeated characters.

# Check if a string has repeated characters using a for loop

You can also use a for loop to check if a string contains repeated characters.

main.py
my_str = 'bobby' def has_repeated_chars(string): for char in string: if string.count(char) > 1: return True return False print(has_repeated_chars('bobby')) # ๐Ÿ‘‰๏ธ True print(has_repeated_chars('abc')) # ๐Ÿ‘‰๏ธ False

check if string has repeated characters using for loop

The code for this article is available on GitHub

On each iteration, we use the str.count() method to check if the character appears more than once in the string.

If the condition is met, we return True and exit the function.

If the condition is never met, the string doesn't contain any repeated characters and False is returned.

# Find the duplicate characters in a String

You can use a for loop if you need to find the duplicate characters in a string.

main.py
def find_duplicate_chars(string): duplicate_chars = [] for char in string: if string.count(char) > 1 and char not in duplicate_chars: duplicate_chars.append(char) return duplicate_chars print(find_duplicate_chars('bobby')) # ๐Ÿ‘‰๏ธ ['b'] print(find_duplicate_chars('abc ac')) # ๐Ÿ‘‰๏ธ ['a', 'c']
The code for this article is available on GitHub

We initialized an empty list and used a for loop to iterate over the string.

On each iteration, we check if the character is contained at least twice in the string.

If the character is contained at least twice in the string and is not in the duplicate_chars list, it gets added to the list.

The function returns a list containing the duplicate characters in the string.

# 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