="John Belushi"
my_nameprint(my_name[0])
print(my_name[len(my_name)-1])
print(my_name[-1])
J
i
i
We’ve been using strings in each of the previous modules, but we’ve accepted them as a found artifact without really getting into them and seeing how they. This module will correct that deficiency and make all of better peopleprogrammers.
Let’s take a quick look at what we’ve done so far.
String literals: "Doug McKenzie"
String variables: comedic_genius = "Mel Brooks"
Comparison: if my_name == your_name:
if your_name != "Chuck Woolery":
Concatenation: full_name = first_name+last_name
Repetition: "ABC" * 20
There is a lot more we can do with strings. We can: * Index into them * Iterate over them * Slice them * Search them * Call methods that act on them…
In fact, when you look at what you can do with a string and how you do it, you suddenly realize that a string is just (conceptually) a tuple of letters.
We can index into a string:
Strings are iterables:
Just like a tuple, the elements of a string are immutable. Once a string is created, the characters can’t be changed.
We can slice strings:
We can search into a string with the in
operator:
found one!
There are a huge variety of string methods. We’ll look at just a few here. Some of them are handy for validating inputs…