A string is a sequential data type, because characters follow each other:

str = "Hello world!"            # creates a string


String with characters and indexes

An index in square brackets [index] is used to access individual elements of the sequential data types:

print(str[4])                   # outputs 'o'


or in Python, access from behind with a negative index is conveniently also possible:

print(str[-2])                  # outputs 'd'

A kolon : is used, when chunk of more than one character is to be extracted [start position:end position-1]:

print(str[6:11])                # outputs from position 6 to 10 -> 'world'
print(str[6:])                  # outputs from position 6 to the end -> 'world!'
print(str[:5])                  # outputs from beginning to position 4 -> 'Hello'


Last modified: Sunday, 27 October 2019, 12:30 PM