简体   繁体   中英

Find position of vowels in a string in Python

I'm trying to work on some Python code where a person is prompted to input a string of text. Then I need to find the position of all of the vowels in the string. I have this, but it's not working...

userInput = (input("Enter a line of text: ")
vowels = ("aeiouAEIOU")
position = 0
for char in userInput :
    if char in vowels :
        position = userInput.find(vowels)
        print(char, position)

It returns the vowels but gives each position as -1. What am I doing wrong? I have read that the index function could be used, but we're not getting to that for a few more weeks. Any suggestions on simple fixes I could make to this code? Thank you!!

Your code has the mistake that when you do userInput.find(vowels) remember that the string vowels is "aeiouAEIOU" so it will not find that unless the either string "aeiouAEIOU" is in userInput . Instead, it is best to enumerate and return those indexes.

userInput = input("Enter a line of text: ")
vowels = "aeiouAEIOU"
for i, char in enumerate(userInput):
    if char in vowels:
        print(char, i)

You can do this with a list comprehension and enumerate:

positions = [i for i, char in enumerate(userInput) if char in vowels]

This will give you a list of the indices of vowels - it enumerates your user input string as a list of characters with an index, and applies a predicate- in this case if the character is a vowel or not.

Once the test char in vowels is verified, you are currently reading a letter char that is a vowel , at that point you can output it directly. On the other hand you need to remember the position by incrementing it every time you move to the next char :

userInput = "This is some input from the user"
vowels = "aeiouAEIOU"
position = 0
for char in userInput:
    if char in vowels:
        print(char, position)
    position += 1

This code can be improved to be a bit more pythonic, using enumerate can save you from keeping track of the position by hand:

serInput = "This is some input from the user"
vowels = "aeiouAEIOU"
for position, char in enumerate(userInput):
    if char in vowels :
        print(char, position)

Another improvement can be made, this time we can improve performances. Time cost of checking char in vowels is proportional to the size of the string vowels . On the other hand you can change the type of vowels from string to set , checking if an item is part of a set is done in constant time:

userInput = "This is some input from the user"
vowels = set("aeiouAEIOU")
for pos, char in enumerate(userInput):
    if char in vowels:
        print(char, pos)
string find(str,str, beg=0, end=len(string)) 

method determines if string str occurs in string, or in a substring of string if starting index beg and ending index end are given. In your code, userInput.find(vowels) it would check the userInput if it contains the whole bunch of vowels which is "aeiouAEIOU". so the code can be improved as follow:

userInput = (input("Enter a line of text: ")
vowels = ("aeiouAEIOU")
position = 0
for char in userInput :
    if char in vowels :
        position = userInput.find(char)
        print(char, position)

Try the below code, it's similar to yours:

 userInput = input("Enter a line of text: ")
 vowels = "aeiouAEIOU"

 for count in userInput:
     x = 9 #there are 10 vowels, from 0 to 9
     while x >= 0:
         if count == vowels[x]:
             print("\n",count)
         x -= 1

 print("\n Done")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM