sec01 - String index

In this lecture, we will learn about string indexes.

String index

Python manages character information by assigning element numbers (indexes) in order from left to right.

For example, the string “I'm Nico, 10.” is managed with indexes as follows.

[0][1][2][3][4][5][6][7][8][9][10][11][12]
I'm Nico, 10.

The ‘[n]’ in the upper row of the table represents the element number (index), indicating the order of each element (in this case, characters) managed by Python. In the human world, numbering starts from 1, but in the computer world, it starts from 0. Therefore, the above string consists of 13 elements, from element number [0] to [12].

(It may not seem easy at first because the element numbers are different from how humans count, but you will get used to it as you write more code.)

In Python, each element of a string is managed as a single character, and symbols such as numbers and spaces are also managed as a single element. Characters such as kanji and hiragana are also managed as one character per element.

Get individual characters from a string (index notation)

Here, we will learn how to extract elements from a string.

Index specification

We learned about indexes. Python can extract specific characters by specifying the index.

Now, let's extract the first character (index [0]) and the 11th character (index [10]) from the string we saw earlier.

s = "I'm Nico, 10."
print(s[0])  # "I"
print(s[10])  # "1"

When you run the above code, I and 1 are output. Using the format variable_name[index], you can extract the character at the specified index from the string stored in that variable.

Use negative values to specify elements

In Python, you can specify a negative value for the element number to extract an element. [-1] specifies the “last element” of the string. Similarly, [-2] specifies the “second-to-last element” of the string, and [-3] specifies the “third-to-last element” of the string...

[0][1][2][3][4][5][6][7][8][9][10][11][12]
[-13][-12][-11][-10][-9][-8][-7][-6][-5][-4][-3][-2][-1]
I'm Nico, 10.

Now, let's extract I and 1 using negative index numbers.

s = "I'm Nico, 10."
print(s[-13])  # "I"
print(s[-3])  # "1"

When you run the above code, I and 1 are output in the same way as before.

Range of indexes that can be specified

When extracting characters by specifying an index, you must specify the index value within the range of that character string. Specifying any other index will result in an error.

The maximum index for I'm Nico, 10. is [12], so specifying a number greater than 13 will result in an error. Try executing the following code.

s = "I'm Nico, 10."
print(s[13])

When executed, the following error message appears. The message reads, “IndexError: string index out of range.” This error occurs not only when a positive number is specified, but also when a negative value is specified and the element number is out of range.

Check the number of elements

To avoid specifying element numbers outside the range, you can use the “len()” function to find out the number of elements in a string. ‘len’ is short for “length.”

Try writing the following code and executing it.

s = "I'm Nico, 10."
print(len(s))

The output result is 13. Since the number of characters is 13, the last element number is [12], which is len() minus 1.

If you want to use len() to get the last character, write the code as follows.

s = "I'm Nico, 10."
last_chr = s[len(s)-1]
print(last_chr)

By performing the calculation len(s)-1 within square brackets, you can obtain the last index.

(※ In Python, you can use the notation [-1], so you don't need to write it this way. However, other programming languages may use this notation, so we have included it here for reference.)