What is the syntax to return a substring?

#to return a single character
#use (-) sign to reverse order of index, keep in mind that -0 and 0 are the same
"<your string here>"[<index of the character you want to return>]

#to return part of a string
"<your string here>"[<start index, inclusive>:<end index, exclusive>]
#examples
>>> str="ABCDEFG"

>>> str[0]
'A'
>>> str[-1]
'G'
>>> str[2]
'C'
>>> str[:]
'ABCDEFG'
>>> str[:3]
'ABC'
>>> str[3:]
'DEFG'

 

 

Advertisements