Why isn't this code bit printing the characters with even index in the string properly? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

Why isn't this code bit printing the characters with even index in the string properly?

Please point out the mistake. https://code.sololearn.com/ciwoAhIv2sBL/?ref=app

5th Jun 2020, 9:03 AM
Noob Gamer
Noob Gamer - avatar
3 Answers
+ 5
Noob Gamer The index() returns the index of first occurrence of specified element. So, in the case when the input is "see", the output is "s" because the first occurrence of "e" is at index 1 and when you say '_str.index("e")' it always returns 1 thereby making your condition False.
5th Jun 2020, 9:13 AM
Kiran Deep Naidu
Kiran Deep Naidu - avatar
+ 6
As index in python is zero based, and zero is an even number, this code can be used: a = 'abcdefghijkl' print(', '.join([char for ind, char in enumerate(a) if ind %2 == 0])) # output: a, c, e, g, i, k (????? ??????, it seems that your code gives output from odd index)
5th Jun 2020, 10:10 AM
Lothar
Lothar - avatar
+ 3
Thanks everyone.
5th Jun 2020, 11:36 AM
Noob Gamer
Noob Gamer - avatar