How do i index a python string?



  • In python indexing starts at zero, so this will print the letter "o" in the word Ahoy.

    indexthis = "Ahoy!"[2]
    print(indexthis)
    

    index the letter y

    indexthis = "Ahoy!"[3]
    print(indexthis)
    

    You can also index a python string using negative numbers, meaning start from the right side of the string instead of the left.

    This will index the ! of the string.

    indexthis = "Ahoy!"[-1]
    print(indexthis)
    

    This will index the h of the string.

    indexthis = "Ahoy!"[-4]
    print(indexthis)
    

Log in to reply
 

© Lightnetics 2024