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
  SyntaxError: unexpected character after line continuation character siteshkumar 2 357 Jul-13-2020, 07:05 PM
Last Post: snippsat
  String index out of bounds ( Python : Dict ) kommu 2 432 Jun-25-2020, 08:52 PM
Last Post: menator01
  Remove from end of string up to and including some character lbtdne 2 483 May-17-2020, 09:24 AM
Last Post: menator01
  String index out of range - help please DudleyDiccle 7 653 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 605 Jan-05-2020, 11:50 AM
Last Post: vivekagrey
  Unicode string index problem luoheng 6 659 Nov-23-2019, 03:04 PM
Last Post: luoheng
  python gives wrong string length and wrong character thienson30 2 678 Oct-15-2019, 08:54 PM
Last Post: Gribouillis
  IndexError: string index out of range ? Q_Y 5 1,859 Jul-05-2019, 07:18 PM
Last Post: noisefloor
  Find string and add character - newbi PyDK 1 541 May-15-2019, 01:22 PM
Last Post: ichabod801
  remove string character from url jacklee26 10 1,934 Mar-25-2019, 03:56 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

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