Skip to content

Accessing Strings

Accessing a Subset of Strings

You can access characters in in a string using their index or a subset of characters through slicing.

Accessing individual characters in a string

You can access individual characters in a string by through their index.

example of accessing a character in a string
name = "Python"
# prints a character at index or position 0
print(name[0]) # outputs: P
# prints a character at index 5
print(name[5]) # outputs: n

Accessing a subset of characters in a string

You can access a subset of characters through slicing.

example of accessing substring
country = "Netherland"
# print the last four characters
print(country[6:]) # output: land
# print the first 6 characters
print(country[0:5]) # outputs: Nether