# Find All Occurrences Of A Character In A String Python

Last Updated : Mar 11, 2024

In this article we will show you the solution of find all occurrences of a character in a string python, let's examine the idea of the Python command to discover every instance of a character inside a string.

A substring is a continuous sequence of characters contained within a String. For instance, a substring of a substring is found inside a paragraph.

A string, which is in Python an array of byte that represent Unicode characters, is one of the most used kinds of information to record data in a way that is intelligible by humans.

Step By Step Guide On Find All Occurrences Of A Character In A String Python :-

### Method - Using Iteration and get method

inp_str = "Talkerscode"
freq = {}
for ele in inp_str:
freq[ele] = freq.get(ele, 0) + 1
print ("Occurrence of all characters in Talkerscode is :\n "+ str(freq))
1. We observe that we have written Python code to find every instance of a character with in given string.
2. You can see that we are using the frequency dictionary and initialise the string in the first line of code.
3. It accomplishes this by repeatedly going through the string's characters and recording a count of every one in a dictionary named freq.
4. The number of all that character with in dictionary is then increased by 1 by using the for loop, which we apply for each character.
5. The dictionary with the character counts is printed at the conclusion.
6. To discover every instance of a characters in a string in Python, we utilise the print function at the conclusion of the code.
7. This approach is O(n) time complicated since the input string is n characters long. sThis is due to the for loop's O time complexity and the get() method's ability to traverse over all of the string's elements (1).
8. The input string's unique character count, k, determines how complex this code's space complexity will be. This is so that the frequency dictionary can keep track of the number of distinct characters in the string.

### Method - using the setdefault () methods

string = "Talkerscode"
freq = {}
for char in string:
freq.setdefault(char, 0)
freq[char] += 1
print ("Occurrence of all characters in Talkerscode is :\n "+ str(freq))
1. As you can see, we use the setdeafult method to construct the Python code necessary to locate every instance of a particular character within a string.
2. The string is initialised and the dictionary frequency is defined right here at the beginning of the code.
3. After that, the Talkerscode string's characters are iterated over in a for loop.
4. The value of a text in the dictionary is then set to 0 using the setdefault() method if it does not already exist.
5. The character's value is then raised in the lexicon by 1.
6. When the loop is finished, the freq dictionary holds the number of times each character appears in the string.
7. In the end of the code we use print function in python for printing the occurrences of all characters in Talkerscode.
8. Time Complexity of this code is O(n) as we are the traversing the completes string where n is the length of the string
9. The space complexity of this code is O(k), where k is the number of unique characters in the input string. This is because the frequency dictionary stores the count for each unique character in the string.

## Conclusion :-

As a result, we had successfully learned how to use Python to find every instance of a characters in a string.

We also learnt how to use the Python setdeafult function and the iteration and get methods to discover every instance of a characters in a string.

I hope this article on find all occurrences of a character in a string python helps you and the steps and method mentioned above are easy to follow and implement.