-1
a=input("type sentence: ") for i in range(1, 100): if a[i] == 'a': print (a[i]) elif a[i] == 'o': break 

According to my experience, in C, we can use a[i] with for to check each index of a string array from a[0] to a[i]. However, whenever I use the same method in Python I get the error "IndexError: string index out of range". What is the problem in this case?

My goal is to check if the sentence I wrote has vowels and if it does, print the first vowel. I tried listing, but problem got worse.

2
  • 3
    Trying to access an index beyond the length of the string will produce this error, yes.
    – deceze
    CommentedDec 4, 2023 at 8:11
  • 2
    If your sentence is shorter than 100 characters it will produce that error. Make sure that the range is no longer than your sentence.
    – erik
    CommentedDec 4, 2023 at 8:13

4 Answers 4

0

Use

for i in range(len(a)): ... 

to avoid the IndexError when i is after the end of the string a.

Even better:

for c in a: if c == 'a': ... 
0
    0

    I suspect the input you gave isn't 100 characters long.

    The problem lies in the fact that the input function in the first line creates the string object in a. This string object will be as long as the input. So by checking for a[100] on a string that is only 5 characters long will result in an IndexError.

    How about trying

    for character in a: if(...): ... 

    edit: Checking for vowels can be easier too.

    list_of_vowels = [a, e, i, o, u] if (character in list_of_vowels): do_something(character) 
      0

      It is very easy in Python, we do not need to compare each index. Just need one comparison using in operator.

      a=input("type sentence: ") for i in a: if (i in ["a","e","i","o","u"]): print (i) break 
        0

        I guess you might be doing this in C:

        char a[100]; scanf("%s", a); 

        In C you need to pre-allocate a fixed-length "container" to later save the input string, and the length should be larger than that of expected input to be able to put the string in a.

        However, in Python you don't need to do so. Python helps you do this extra work. In Python, executing

        a = input() 

        and you will get a str variable a which contains your input string. The length of a is automatically determined by the length of your input string, and you can get the length by using the len function.

        So the version of your code that works should be

        for i in range(1, len(a)): if a[i] == 'a': print (a[i]) elif a[i] == 'o': break 
        1
        • Also, in Python the indexing also starts from 0, not 1. So if you want to loop from the first letter of your string to the last instead of from the second letter to the last, you should consider change range(1, len(a)) to range(0, len(a)) or range(len(a)) (which is the same as range(0, len(a))).
          – Terence
          CommentedDec 4, 2023 at 8:20

        Start asking to get answers

        Find the answer to your question by asking.

        Ask question

        Explore related questions

        See similar questions with these tags.