Python: Count the number of occurrence of a specific character in a string
Character Frequency Counter
Write a Python program to count the number of occurrences of a specific character in a string.
Pictorial Presentation:
Sample Solution:
Using count() function:
Python Code:
Sample Output:
Original string: The quick brown fox jumps over the lazy dog. Number of occurrence of 'o' in the said string: 4
Using loop:
Python Code:
Sample Output:
Original string: The quick brown fox jumps over the lazy dog. Number of occurrence of 'o' in the said string: 4
Flowchart:
Using collections.Counter():
Python Code:
Sample Output:
Original string: The quick brown fox jumps over the lazy dog. Number of occurrence of 'o' in the said string: 4
Using Lambda functions:
Python Code:
Sample Output:
Original string: The quick brown fox jumps over the lazy dog. Number of occurrence of 'o' in the said string: 4
Using Regular Expressions:
Python Code:
Sample Output:
Original string: The quick brown fox jumps over the lazy dog. Number of occurrence of 'o' in the said string: 4
For more Practice: Solve these Related Problems:
- Write a Python program to count the occurrences of each character in a string, ignoring case.
- Write a Python program to find the most frequently occurring character in a string.
- Write a Python program to count the occurrences of a given character only within a specified portion of a string.
- Write a Python program to replace all occurrences of the most frequent character with a different one.
Go to:
Previous: Write a Python program to test if a certain number is greater than all numbers of a list.
Next: Write a Python program to check if a file path is a file or a directory.
Python Code Editor:
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.