Drop links or images here to add them to the editor.

A string is a row of characters, and every character in the row has a position. That makes it possible to pick out one character, or a whole stretch of them. Part of a string is called a substring.

Every character has an index

Python numbers the characters in a string exactly the way it numbers the items in a list. The number is called the index, and the counting starts at zero:

Index 0 1 2 3 4 5
Character P y t h o n
Counting from the end -6 -5 -4 -3 -2 -1

One character is picked out with its index in square brackets, just like one item from a list:

word = "Python"

print(word[0])
print(word[3])

That prints P and then h. The last character of a six-character string is at index 5, one less than its length, for the same reason the last item of a list is.

The bottom row of the table is the other way of counting. A negative index counts from the end of the string instead of the start: word[-1] is the last character, n, and word[-2] is the one before it. That is handy when you want the end of a string and do not know how long it is.

Slicing: a whole stretch at once

To get more than one character, put two numbers in the square brackets with a colon between them. This is called slicing:

subject = "Computer Science"

print(subject[0:8])

That prints Computer. The first number is the index where the substring starts. The second is where it stops, and the character at that index is not included. subject[0:8] gives you the characters at indices 0 up to and including 7, which is the eight letters of Computer, and it stops before the space at index 8.

The stop index is left out, exactly like the stop value of range. So to slice from a start position up to and including an end position, the second number has to be one more than the end position:

string[start_position:end_position + 1]

subject[3:6] gives put: the characters at 3, 4 and 5, and not the one at 6. A handy check is that the number of characters you get is always the second number minus the first: 6 minus 3 is 3 characters.

From the start, or to the end

Either number can be left out.

Leave out the first and the slice starts at the very beginning. Leave out the second and it runs all the way to the end:

subject = "Computer Science"

print(subject[:4])
print(subject[9:])

That prints Comp and then Science.

Two slices come up so often that other languages, and the pseudocode in exams, give them names of their own. Left takes a number of characters from the start of a string, and right takes a number of characters from the end. Python has no left or right, but a slice does the same job:

Pseudocode Python What you get
.left(number_of_characters) string[:number_of_characters] that many characters from the start
.right(number_of_characters) string[-number_of_characters:] that many characters from the end

Pay attention to where the colon is! For the left of a string the colon comes first, followed by how many characters you want. For the right of a string the colon comes last, after a negative number that says how far back from the end to start.

subject = "Computer Science"

print(subject[:4])
print(subject[-7:])
print(subject[-3:])

subject[:4] is the left four characters, Comp. subject[-7:] is the right seven, Science, and subject[-3:] is the right three, nce. Move the colon to the other side of the number and you get something quite different, so try it and see.

Slicing a variable

The numbers inside the square brackets can be variables, and the string can be anything a variable holds, including something the user typed:

print("Enter your name:")
name = input()

print("Your first letter is " + name[0])
print("Your first three letters are " + name[:3])
print("Your last letter is " + name[-1])
Help! My code doesn't work!

Make sure that you check for the following things:

  • The index or slice is inside square brackets, straight after the string.
  • String indexing starts at zero. Are you counting properly?
  • Spaces and punctuation have an index too, so count them.
  • The stop number of a slice is never included, so add 1 to it if the character there should be.
  • The colon is on the correct side of the number: [:4] for the left, [-4:] for the right.
  • Asking for a single character past the end of the string, such as word[6] for Python, stops the program with an IndexError.