mardi 26 octobre 2021

How do I print the percentage of a letter in a word? - Python [closed]

I made this program that reads the user's string input, then asks for a character to be removed, and then prints the input string without the character the user chose.

After that, asks the user to enter a character, and the code counts and prints, how many of this letter are in the string.

So the thing I'm struggling with is, how do I give to the user the percentage of that letter. ex. banana contains 3 a's which is 50%.

Input

s = input ('Enter a string: ')

if s == '': 
    print ('Empty String, please enter a string: ')
else:
    d = int(input('Please enter the character to be removed: '))

    print('The new string is: ', s[:d] + s[d+1:])

l = input ('Enter a character: ')
count = 0

for i in s:
    if l == i:
        count += 1
print(count)

Output

Enter a string: banana
Please enter the character to be removed: 0
The new string is: anana
Enter a character: a
Count: 3

Aucun commentaire:

Enregistrer un commentaire