Quick actions

cmd+k|ctrl+k

Navigation

Languages

string-indexing&slicing

Snippet info

Language

Python

Visibility

public

Author

narutouzumakiramen18

Created

2026-05-02T18:19:06.919859Z

Updated

2026-05-02T18:25:19.332098Z

# we access a piece of string using a index.
# since every char/element inside a sting is stored in memory bit by bit.
# we can say we are asking python for the specific bit by giving python a token number in the form of a index.

selfish = "me me me"
        #  012347  
# [start:stop] 
print(selfish[0])
print(selfish[-1])

# [start:stop:step] step over a few elements.
selfish_2 = '01234567'
print(selfish_2[0:6]) # upto number 5

# start from the beginning and end at a specific point
print(selfish_2[:5])
# start from a specific point and end at the end of the sequence
print(selfish_2[2:])
# To jump alternating numbers in between
print(selfish_2[::2])

# To reverse a sequence
print(selfish_2[::-1])
INFO