Python Forum
How to get the index of a character from a string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get the index of a character from a string
#1
Hi, i used this code it is getting the index value but i need it to get the index value as the expected output
s = 'Apples are totally awesome'
l = []
for idx, item in enumerate(s.lower()):
if 'a' in item:
l.append(idx)
if('l' in item) and ('a' in s[idx-1]):
l.append(idx)
print(l)

Actual output:
[0,7,14,15,20]

Expected output:
[0,7,14,15,16,20]
Reply
#2
can you explain the desired output in plain english
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Writing string to file results in one character per line RB76SFJPsJJDu3bMnwYM 4 1,306 Sep-27-2022, 01:38 PM
Last Post: buran
  I'm getting a String index out of range error debian77 7 2,280 Jun-26-2022, 09:50 AM
Last Post: deanhystad
  Regex: a string does not starts and ends with the same character Melcu54 5 2,368 Jul-04-2021, 07:51 PM
Last Post: Melcu54
  [solved] unexpected character after line continuation character paul18fr 4 3,296 Jun-22-2021, 03:22 PM
Last Post: deanhystad
  string index out of range jade_kim 4 2,565 Jan-13-2021, 05:41 AM
Last Post: jade_kim
  SyntaxError: unexpected character after line continuation character siteshkumar 2 3,110 Jul-13-2020, 07:05 PM
Last Post: snippsat
  String index out of bounds ( Python : Dict ) kommu 2 2,351 Jun-25-2020, 08:52 PM
Last Post: menator01
  Remove from end of string up to and including some character lbtdne 2 2,286 May-17-2020, 09:24 AM
Last Post: menator01
  String index out of range - help please DudleyDiccle 7 3,310 Mar-27-2020, 12:10 AM
Last Post: DudleyDiccle
  how can i handle "expected a character " type error , when I input no character vivekagrey 2 2,675 Jan-05-2020, 11:50 AM
Last Post: vivekagrey

Forum Jump:

User Panel Messages

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