close

How to count Occurrences of a Character in a String in Python

Hello guys. How are you all? I hope you all fine. In this tutorial we will learn about how to count Occurrences of a Character in a String in Python. so without wasting time lets learn about of this.

How to count Occurrences of a Character in a String in Python

  1. count Occurrences of a Character in a String in Python

    to count Occurrences of a Character in a String in Python just use count(). By using count() you can count Occurrences of a Character in a String in Python. So lets learn about of this without wasting time by given below example: print('I have two students'.count('t')) Output : 3

  2. How to count Occurrences of a Character in a String in Python

    to count Occurrences of a Character in a String in Python just use findall(). By using findall() you can count Occurrences of a Character in a String in Python. So lets learn about of this without wasting time by given below example: print('I have two students'.count('t')) import re mystr = "I have two students" print(len(re.findall("t", mystr))) Output : 3

Method 1: count Occurrences of a Character in a String Using count()

By using count() you can count Occurrences of a Character. So lets learn about of this without wasting time by given below example:

print('I have two students'.count('t'))

Output :

3

Method 2 : Use findall()

By using findall() you can count Occurrences of a Character. So lets learn about of this without wasting time by given below example:

print('I have two students'.count('t'))
import re
mystr = "I have two students"
print(len(re.findall("t", mystr)))

Output :

3

Conclusion

It’s all About this Tutorial. Hope all methods helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which method worked for you?

Also, Read

Leave a Comment