Last updated: Apr 9, 2024
Reading timeยท3 min
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.
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
The str.count() method returns the number of occurrences of a substring in a string.
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.
You can use the same approach to check if a character appears more than once in a string.
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')
The character appears more than once in the string, so the if
block runs.
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.
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
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
.
set
is not equal to the string's length, then the string has repeated characters.You can also use a for loop to check if a string contains repeated characters.
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
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.
You can use a for
loop if you need to find the duplicate characters in a
string.
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']
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.
You can learn more about the related topics by checking out the following tutorials: