How do I index a range in python string?



  • Also see: How do i index a python string?

    #!/usr/bin/python
    indexthis = "Enter the dragon"[0:3]
    print(indexthis)
    

    The output

    $ ./main.py
    Ent
    

    The range can be given in the print function instead of the variable.

    #!/usr/bin/python
    indexthis = "Enter the dragon"
    print(indexthis[0:3])
    

    Leaving out the last digit from the range, i.e. [0:] range will print 0 to the end of the string. result: Enter the dragon

    Leaving out the first digit from the range, i.e. [:4] range will print 0 to 4. result: Ente

    Leaving both digits out, i.e. [:] will copy the string.


Log in to reply
 

© Lightnetics 2024