Home > front end >  How to select certain characters in a string in Python?
How to select certain characters in a string in Python?

Time:05-29

My name is Shaun. I am 13 years old and trying to learn python.

I am trying to make a program that finds vowels in an input and then prints how many vowels there are in the input the user gives.

Here is the code:

s = (input('Enter a string: ')) # Let the user give an input (has to be a string)

Vwl = [] # Create an array where we will append the values when the program finds a vowel or several vowels

for i in s: # Create a loop to check for each letter in s 
   count_a = 0 # Create a variable to count how many vowels in a

   count_e = 0 # Create a variable to count how many vowels in e

   count_i = 0 # Create a variable to count how many vowels in i

   count_o = 0 # Create a variable to count how many vowels in o

   count_u = 0 # Create a variable to count how many vowels in u

The function below is pretty long to explain, so summary of the function below is to find a vowel in s (the input) and make one of the counters, if not some or all, increase by 1. For the sake of learning, we append the vowels in the array Vwl. Then, it prints out Vwl and how many letters there are in the list by using len.

   if s.find("a" or "A") != -1: 
       count_a = count_a   1    
       Vwl.append('a')          

   elif s.find("e" or "E") != -1:
       count_e = count_e   1
       Vwl.append("e")
   elif s.find("i" or "I") != -1:
       count_i = count_i   1
       Vwl.append("i")
   elif s.find("o" or "O") != -1:
       count_o = count_o   1
       Vwl.append("o")
   elif s.find("u" or "U") != -1:
       count_u = count_u   1
       Vwl.append("u")

  print(Vwl)
  print(f"How many vowels in the sentence: {len(Vwl)}")

For some odd reason however, my program first finds the first vowel it sees, and converts the whole string into the first vowel it finds. Then it prints down the wrong amount of vowels based on the array of vowels in the array Vwls

Could someone please help me?

CodePudding user response:

The reason your code only prints out the first vowel is because the if statements you coded are not inside a loop, that part of the code runs once and then it finishes, so it only manages to find one vowel. Here are couple ways you can do what you are trying to do:

Way 1: Here is if you just want to count the vowels:

s = input()
vowel_counter = 0
for letter in s:
    if letter in "aeiou":
        vowel_counter =1
print(f"How many vowels in the sentence: {vowel_counter}")

Way 2: Use a python dictionary to keep track of how many of each vowel you have

s = input()
vowel_dict = {}
for letter in s:
    if letter in "aeiou":
        if letter not in vowel_dict:
            vowel_dict[letter]=0
        vowel_dict[letter] =1
print(f"How many vowels in the sentence: {sum(vowel_dict.values())}")
print(vowel_dict)

CodePudding user response:

The find function doesn't work here, because it loops the whole string and returns the position of the first character it finds.

  • Related