Count Occurrences of Letters in Text/String With Python | Frequency of Letters in String in Python

preview_player
Показать описание
⬇️ *LEARN ON THE BEST LEARNING PLATFORMS (LINKS BELOW)* 😉💪 ⬇️

*SKILLSHARE*
_(Python, Web Dev, UI/UX Design, Music, Art, Animation and a lot more)_

*DATACAMP*
_(Python, ChatGPT, SQL, Power BI, and a lot more)_

*COURSERA PYTHON*
_(For beginners, Data Science, Data Analysis, AI, Cybersecurity and a lot more):_

*COURSERA WEB DEVELOPMENT*
_(Full Stack, Front-End, Back-End, Web Design and a lot more):_

Create a python program to count the number of occurrences of each character in a string and calculate what percentage it is of the total (frequency). This tool could be used to break substitution ciphers like the Caesar Cipher.

🎥🔥 RECOMMENDED VIDEOS TO WATCH 🎥🔥

If the video was helpful, let me know in the comments down below and also like the video so that other people can see it. Needles to say I really appreciate your support 💪❤️

📨 SUBSCRIBE so that you don't miss any new video:

🔗 RELATED CONTENT (videos, playlists, etc) 🔗

🤩 AFFILIATE LINKS 🤩

🧑🏻‍💻 ABOUT ME 🧑🏻‍💻

I'm Fabio, I started programming with Python a lot of years ago and I fell in love with this world.
I started this Youtube channel to help you during your own journey and I hope that my help can make you an amazing programmer who loves this world as I do.
Welcome on board!! 🚀

#pythonWithFabioMusanni #python #pythontutorial #pythonprojects #pythonprogramming #pythonforbeginners #coding #programming
Рекомендации по теме
Комментарии
Автор

⬇️ *LEARN ON THE BEST LEARNING PLATFORMS (LINKS BELOW)* 😉💪 ⬇️

*SKILLSHARE*
_(Python, Web Dev, UI/UX Design, Music, Art, Animation and a lot more)_

*DATACAMP*
_(Python, ChatGPT, SQL, Power BI, and a lot more)_

*COURSERA PYTHON*
_(For beginners, Data Science, Data Analysis, AI, Cybersecurity and a lot more):_

*COURSERA WEB DEVELOPMENT*
_(Full Stack, Front-End, Back-End, Web Design and a lot more):_

Thank you for the support!❤

FabioMusanni
Автор

Excellent video! I am making a Scrabble-like game and these kinds of videos are exactly what I need

koyha
Автор

Such great content!!! Thanks a lot for sharing 😀

danimatviolin
Автор

How do you have so few subs ? Your content is so great, keep it up !!!

boudescotch
Автор

is this for to find an item? iam trying to understand this code is use for?

pro.empire
Автор

Very good problem. I have done it a little different using various modules and concepts. Here's the code:

from collections import Counter, defaultdict
from string import ascii_lowercase
text = """
I'm Fabio, I started programming with Python a lot of years ago and I fell in love with this world.
I started this Youtube channel to help you during your own journey and I hope that my help can make you an amazing programmer who loves this world as I do.
Welcome on board!!
"""

print(text)
total_chars = len(text)
print(total_chars) # total chars in the text
print(ascii_lowercase) # lowercase letters using string module

occur_counter = Counter(text.lower()) # using Counter from collections module
print(occur_counter)
for key in list(occur_counter.keys()): # removing non-letter chars
if key not in ascii_lowercase:
del occur_counter[key]
print(occur_counter)
total_chars_letters = sum(occur_counter.values()) # total chars in the text of letters
print(total_chars_letters)

total_chars_letters = 0
occur_dict = defaultdict(int) # another way using defaultdict from collections module
for ch in text.lower():
if ch in ascii_lowercase:
occur_dict[ch] += 1
total_chars_letters +=1 # calculating total chars in the loop itself, can use sum as before

print(occur_dict)
print(total_chars_letters)

### get_percentage = ((key, for key, val in sorted(occur_dict.items(), key=lambda x: x[1], reverse = True)) # this is a generator expression

def get_percentage(): # this is generator function which is readable similar to generator expression
for key, val in sorted(occur_dict.items(), key = lambda x: x[1], reverse = True): # using lambda function in key parameter of sorted function to sort according to value in the dict
yield key, (val/total_chars_letters)*100 # yield the percentage

for item in get_percentage(): # generator returns an iterator so loop over it
print(f'%ge of "{item[0]}": {item[1]:.4f}')

print(occur_counter == occur_dict) # just to verify if key value pairs are equal in both Counter and defaultdict

venkataramanapaik
visit shbcf.ru