Lesson 02-02: Specific Letters with String Indexing

Learning Target: I can use the len() function to find the length of a string.
Learning Target: I can use string indexing to reference specific letters in a string.

Length of a String

The len() function is a function that will provide the length of a string. The length of a string is basically the number of characters in the string, spaces included. Here are a few quick examples:

 
1
print(len("hello!")) # ==> 6
2
print(len("h e y there")) # ==> 11
3
name = "Bobert"
4
print("\n") #ignore this line
5
print("The name {} has {} characters".format(name,len(name)))
6
#should print "The name Bobert" has 6 characters"
7
6
11


The name Bobert has 6 characters

(ex_strlen_1)

Finding a Specific Letter in a String

We can point to a specific character within a string by using string indexing. Every letter, digit, space, or symbol in a string has a position in a string, also known as its index.

We use string indexing to reference a specific character in the string by using its index. The way that this is done is through brackets [] right after the string, with a number inside.

Try to predict what the following code will print:

3
 
1
print("Hello"[4]) #the 4th position of the string
2
print("Hello"[1]) #the 1st position of the string
3

(ex_strindex_1)

Was it what you expected? If this is your first time seeing string indexing, you were probably off by one. That’s because strings use zero-based indexing, which is when the index always starts counting from zero, instead of one.

Here’s an example of the values of word[0], word[3], and word[6] for a variable word = "POKEMON".

0 -> "P", 3 -> "E", 6 -> "N"

It’s worth noting that string indexing gives you a string as the result.

Please keep in mind that from this point on, this book will use 0-based indexing as well. So if you read “the 4th position” or “the 4th letter”, it is the same as saying “the 4th index”

What happens if you reference a position that’s greater than the length of the string itself? Try running the code and see:

3
 
1
word = "POKEMON"
2
print(word[100])
3

(ex_strindex_2)

Trying to reference a position that’s past the end of the string will give you an error! And it even gives us a descriptive error message as well. Use this to your advantage when you come across it.

Typically, to find the last character in a string without knowing what the string is going to be, we would use the expression word[len(word)-1]. But in python, there’s an easier way.

Negative String Indexing

You can provide negative numbers as indexes as well. Its behavior is interesting. See the example below:

5
 
1
word = "abcdef"
2
print(word[-1])
3
print(word[-2])
4
print(word[-3])
5

(ex_negindex_1)

As you can see, when you provide a negative number, it starts counting from the end, with the last letter having an index of -1. This is very important because this allows us to use string indexing to things like “the second to last letter” of a string.

Checks For Understanding

Q#1

ch0202-1: With word = “duck tape”, what would the expression word[6] give us?





Q#2

ch0202-2: With word = “battery”, which ways can we access the last letter? Check all that apply.






Q#3

ch0202-3: With word = “hello world”, which of the following will give an error?




Q#4

In the following area, complete the code so that the output matches what is stated in the comments #.

7
 
1
word = "yoloswag"
2
print("\n")
3
print() # ==> 'o'
4
print() # ==> 'a'
5
print() # the last letter
6
print() # the 3rd to last letter
7

(cfu_strindex_4)

Next Section - Lesson 02-03: Substrings with String Slicing