How can I make my code more compact? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
21st Oct 2022, 8:21 AM
Aege
9 Answers
+ 10
#This does the same thing with less code: a = input() for x in "aeiou": print(f"{x.upper()} letters:\n{a.count(x)}")
21st Oct 2022, 8:54 AM
Brian
Brian - avatar
21st Oct 2022, 8:47 AM
Riya
Riya - avatar
+ 5
I used a technique known as string interpolation. The string is prefixed with the letter f"...". Inside the string there are braces. Whatever is found between the braces gets evaluated as a program expression. The result gets converted to a string and inserted in place. Both, upper (convert to uppercase) and count, are methods supplied by the string class.
21st Oct 2022, 9:40 AM
Brian
Brian - avatar
+ 4
Brian nice explanation 👍
21st Oct 2022, 9:42 AM
Riya
Riya - avatar
+ 4
Hi, just noticed that the code of Aege and Brian won't result in any vowel count if the input is in all capital letters. Thanks.
23rd Oct 2022, 1:33 AM
Cris Tradio
Cris Tradio - avatar
+ 3
Cris Tradio good observation. I noticed it also, but decided to keep my code true to the original. An easy adjustment would be to convert the input string to all uppercase. Now this counts all vowel letters: a = input().upper() for x in "AEIOU": print(f"{x} letters:\n{a.count(x)}")
23rd Oct 2022, 3:36 AM
Brian
Brian - avatar
+ 2
Thanks Brian!
23rd Oct 2022, 4:44 PM
Cris Tradio
Cris Tradio - avatar
+ 1
Yeah, the only problem is that it doesn't recognize which vowel is, thx by the way
21st Oct 2022, 8:53 AM
Aege
0
Wow, which function are you using? How is it called?
21st Oct 2022, 9:03 AM
Aege