Python Forum
Print the index of the vowels in a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Print the index of the vowels in a string
#1
Hello guys,

Here i need to print the index of the vowels in a string

Here is my code
li=['a','e','i','o','u']
word='Rizvi'
for i in word:
    if i in li:
        print word.index(i)
The expected output is:
Output:
1 4
what i am getting is:
Output:
1 1
Thanks in advance
Reply
#2
Yes, str.index is the wrong tool here for a few reasons.
Firstly and the root of your issue here, it doesn't know which occurrence of the character you are looking for so it just returns the index of the first location both times.
Secondly (and probably a little beyond your current knowledge), it is unnecessarily increasing the time complexity of your code. You are running in O(n^2) time for an algorithm that should be O(n).

What you should do is use enumerate.

Observe how I have access to the character and index of each character as I itterate and try to apply that to your issue:
>>> my_string = "Spam and eggs"
>>>
>>> for i,char in enumerate(my_string):
...     print(i, char)
...
(0, 'S')
(1, 'p')
(2, 'a')
(3, 'm')
(4, ' ')
(5, 'a')
(6, 'n')
(7, 'd')
(8, ' ')
(9, 'e')
(10, 'g')
(11, 'g')
(12, 's')
>>>
Reply
#3
str.index(x) method returns only the first appearance of the 'x'.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#4
I changed the code like this!
it works as expect Smile
li=['a','e','i','o','u']
word="Rizvi"
for i in range(len(word)):
   if word[i] in li:
       print i
Output:
1 4
Reply
#5
Good it work,but we really don't like range(len(something)) Undecided
Mekire did hint about enumerate().
li = ['a','e','i','o','u']
word = "Rizvi"
for index,item in enumerate(word):
   if item in li:
       print(index)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to print out the children index of the search tree? longmen 7 2,151 Apr-02-2022, 06:58 AM
Last Post: Yoriz
Bug How to print only vowels from an input? PP9044 8 7,514 Feb-26-2021, 04:02 PM
Last Post: Serafim
  Reading a text until matched string and print it as a single line cananb 1 2,017 Nov-29-2020, 01:38 PM
Last Post: DPaul
  Counting Vowels in Python sapphosvoice 1 3,288 May-05-2020, 04:24 AM
Last Post: buran
  How to print the docstring(documentation string) of the input function ? Kishore_Bill 1 3,541 Feb-27-2020, 09:22 AM
Last Post: buran
  Return not vowels list erfanakbari1 2 2,680 Mar-26-2019, 11:37 AM
Last Post: perfringo
  Help with finding vowels nlord7 1 2,258 Feb-27-2019, 04:40 AM
Last Post: ichabod801
  print string in reverse pseudo 5 3,506 Oct-04-2018, 08:11 PM
Last Post: buran
  String index out of range felie04 2 5,519 Aug-17-2018, 11:18 PM
Last Post: felie04
  Functions to remove vowels and extra blanks RiceGum 10 5,849 Nov-17-2017, 05:40 PM
Last Post: heiner55

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020