Previous Lesson Current Lesson Next Lesson

Question 1

s = "dog"

print(len(s))
print(s[2])

This will output:

3
g

"dog" has only three characters, so the index of the 3rd and last element is 2.

Question 2

template = "My name is {}."

print(template.format("Mike"))
print(template.format("John"))
print(template.format("Anne"))
print(template.format("Sarah"))

This will output:

My name is Mike.
My name is John.
My name is Anne.
My name is Sarah.

We need only one placeholder because we just need one for the name.

Question 3

The __init__ method for str accepts any valid Python object:

s = {1, 2, 3}

s_string = str(s)
print(s_string)

This will output:

{1, 2, 3}