This is an archived version of the course and is no longer updated. Please find the latest version of the course on the main webpage.

Strings are sequences

In fact, a string is also a sequence, more specifically a text sequence.

So you can access individual characters, perform slicing and iterate over strings just as you would do with lists/tuples.

Predict what each of these do before verifying. Note that a space is also a character!

my_name = "Josiah Wang" 

print(len(my_name))

print(my_name[2])
print(my_name[-1])
print(my_name[0:6])
print(my_name[-4:])
print(my_name[0:10:2])
print(my_name[::-1])

for character in my_name:
    print(character)

You can also use the +, *, in and not in operators as in a list.

my_name = "Josiah"

print(my_name + my_name)
print(my_name * 7)

my_name += my_name
print(my_name)

print("x" in my_name)
print("x" not in my_name)

str is immutable, so like tuples you cannot append() to a string.