mardi 26 octobre 2021

How to count a specif letter in word. - Python

Python

I made a 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, I want the user to enter a character of the word and then the code will count how many of the chosen character are in the word. I'm struggling to print the correct answer.

ex. input = banana | character = 0 | print = anana | count character = a | count = 3

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:])

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

for i in s:
        count = 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
6

Aucun commentaire:

Enregistrer un commentaire