Skip to content Skip to sidebar Skip to footer

Find And Print Vowels From A String Using A While Loop

Study assignment (using python 3): For a study assignment I need to write a program that prints the indices of all vowels in a string, preferably using a 'while-loop'. So far I hav

Solution 1:

Try This :

string = input( "Typ in a string: " )
    vowels = ["a", "e", "i", "o", "u"]




        higher_bound=1
        lower_bound=0while lower_bound<higher_bound:
            convert_str=list(string)
            find_vowel=list(set(vowels).intersection(convert_str))
            print("Vowels in {} are {}".format(string,"".join(find_vowel)))

            lower_bound+=1

You can also set higher_bound to len(string) then it will print result as many times as len of string.

Since this is your Study assignment you should look and practice yourself instead of copy paste. Here is additional info for solution :

In mathematics, the intersection A ∩ B of two sets A and B is the set that contains all elements of A that also belong to B (or equivalently, all elements of B that also belong to A), but no other elements. For explanation of the symbols used in this article, refer to the table of mathematical symbols.

In python :

The syntax of intersection() in Python is:

A.intersection(*other_sets)

A = {2, 3, 5, 4}
B = {2, 5, 100}
C = {2, 3, 8, 9, 10}

print(B.intersection(A))
print(B.intersection(C))
print(A.intersection(C))
print(C.intersection(A, B))

Solution 2:

You can get the character at index x of a string by doing string[x]!

i = 0# initialise i to 0 here first!while i < len( string ):
    ifstring[i] in vowels:
        indices += str(i)
    i += 1print( indices )

However, is making indices a str really suitable? I don't think so, since you don't have separators between the indices. Is the string "12" mean that there are 2 vowels at index 1 and 2, or one vowel index 12? You can try using a list to store the indices:

indices = []

And you can add i to it by doing:

indices.append(i)

BTW, your for loop solution will print the vowel characters, not the indices.

If you don't want to use lists, you can also add an extra space after each index.

indices += str(I) + " "

Post a Comment for "Find And Print Vowels From A String Using A While Loop"