v String Indexing - Python

String Indexing

Create a string

string = 'Strings are defined as ordered collections of characters.'

Print the entire string

string[:]
'Strings are defined as ordered collections of characters.'

Print the first three characters

string[0:3]
'Str'

Print the first three characters

string[:3]
'Str'

Print the last three characters

string[-3:]
'rs.'

Print the third to fifth character

string[2:5]
'rin'

Print the first to the tenth character, skipping every other character

string[0:10:2]
'Srnsa'

Print the string in reverse

string[::-1]
'.sretcarahc fo snoitcelloc deredro sa denifed era sgnirtS'