Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
10327 views
ubuntu2004
Kernel: Python 3 (system-wide)

Strings II - indicies and substrings

Accessing a character in a string

Individual characters in a string can be accessed by specifying the character's index in square brackets after the string's name. The first character has index 0, the second character has index 1, and so on. This can be a bit confusing to start with: just remember that to get the index (0, 1, etc.) subtract 1 from its position (first, second, etc).

For instance, the table below shows the indicies of the characters in the string "Hello, world!". The letter "H" is at index 0, the comma is at index 5, the letter "d" is at index 11 and the exclamation mark is at index 12.

stringHello,World!
position1st2nd3rd4th5th6th7th8th9th10th11th12th13th
index0123456789101112
index from end-13-12-11-10-9-8-7-6-5-4-3-2-1

It is also possible to specify characters based on their position relative to the end of the string: the last character can be accessed using index -1, the second to last at index -2, etc., as shown above.

sentence = 'Hello, world!' print( sentence[0] ) # prints the first character (at index 0), i.e. "H" print( sentence[5] ) # prints the sixth character (at index 5), i.e. "," print( sentence[12] ) # prints the thirteenth character (at index 12), i.e. "!" print( sentence[-1] ) # prints the last character (at index -1), i.e. "!"
H , ! !

Accessing several characters: Substrings

Parts of strings (known as substrings) can be accessed using something called the slice operator. Instead of a single index in the square brackets, we use two numbers to refer to the start and end indicies like so: [start index : end index].

The character at the location of the stop index is not included, so [3:7] will access characters from index 3 to 6 (or the fourth to seventh characters).

Where no number is specified before the colon e.g. [:5], this will automatically start from the first character of the string.

Where no number is specified after the colon e.g. [5:], this will automatically end at the last character of the string.

If no numbers are specified then all characters are used.

sentence = 'Hello, world!' print( sentence[7:12] ) # prints the 8th to 12th characters (indicies 7 to 11), i.e. "world" print( sentence[:5] ) # prints the 1st to 5th characters (indicies 0 to 4), i.e. "Hello" print( sentence[7:] ) # prints the 8th to last characters (indicies 7 to 12), i.e, "world!" print( sentence[:] ) # prints all characters (indicies 0 to 12)
world Hello world! Hello, world!

Stepping over characters

The last example using [:] might seem pointless - why not just do print( sentence ) instead? The reason why this is useful will become clear in a moment.

Say we wanted every third character in a string. Then we can use what's called the extended slice notation: [start index : stop index : step size].

To access every third character starting from index 2 and stopping at index 9 we would write

sentence[2:9:3]

where step size is 3.

To access every second character starting from the first character and stopping at the last character we would write

sentence[::2]

where step size is 2.

Try these in the following code.
sentence = 'Hello, world!' print( sentence[2:9:3] ) # prints the 3rd to 9th characters printing every 3rd, i.e. "l,o" print( sentence[::2] ) # prints the 1st to last characters printing every 2nd, i.e. "Hlo ol!"
l,o Hlo ol!

Reversing a string

Now here's the clever bit. If the step size is negative we go through the string backwards. Which means that the notation

sentence[::-1]

reverses the string because step size is -1: We step backwards accessing every character. This is a quick and easy way of reversing strings; something we often want to do with DNA and RNA sequences.

Try this in the code cell below.
sentence = 'Hello, world!' # Reverse the string in sentence and assign it to the variable called rev_sentence rev_sentence = sentence[::-1] print( rev_sentence )
!dlrow ,olleH

Exercise Notebook

Next Notebook