A piece of text in Python is called a string: a string of characters, one
after the other. You have been making strings since your very first print, and
joining them together with + since the chapter on variables.
So far a string has always been handled in one piece. It gets stored, printed or joined onto another string, but nothing ever looks inside it. This chapter is about looking inside: how long a string is, what its letters are, and which part of it you want. Taking strings apart and building new ones out of the pieces is called string manipulation.
len gives you the number of characters in a string, which is called its
length.
text_length = len("Dave")
print(text_length)
That prints 4. It is the same len you used to count the items in a list, and
it does the same job: a list is counted in items, a string is counted in
characters.
Every character counts, not only the letters. A space is a character, and so is a full stop, a digit or an exclamation mark:
print(len("Dave"))
print(len("Ada Lovelace"))
print(len("Hi!"))
print(len(""))
Ada Lovelace has eleven letters and one space, so its length is 12. The last
line measures the empty string, two speech marks with nothing between them,
and its length is 0.
len gives back a whole number, so it can go anywhere a number can go,
including a condition:
username = "dragon"
if len(username) < 3:
print("That username is too short")
else:
print("Welcome, " + username)
dragon is six characters long, so the condition is false and the else
branch runs. This is how a program checks that a password is long enough, or
that a user has typed anything at all.
A letter has two forms, a capital letter and a small one. These are called upper case and lower case, and Python can convert a whole string to either one.
text = "Hello World"
print(text.upper())
print(text.lower())
HELLO WORLD
hello world
upper and lower are written after the string with a full stop in between,
the same way append is written after a list. The empty brackets on the end are
not optional: without them, Python does not run the conversion.
Only letters have a case. Digits, spaces and punctuation come through a
conversion exactly as they went in, so "Room 101!".upper() gives
ROOM 101!.
upper and lower do not change the string you give them. They build a
new string and hand that back, so the result has to be printed or stored
somewhere:
text = "Hello World"
shout = text.upper()
print(shout)
print(text)
That prints HELLO WORLD and then Hello World: text itself is untouched.
If you want the variable to hold the converted version, assign the result back
to it, as in text = text.upper().
Python compares strings exactly, capital letters included, so "yes",
"Yes" and "YES" are three different strings. Users type all three.
Converting the answer before checking it means one comparison covers every way of typing it:
print("Do you want to play again?")
answer = input()
if answer.lower() == "yes":
print("Here we go!")
else:
print("Goodbye")
Run it a few times and answer yes, Yes and YES. All three start the game.
Python can also tell you what case a string is already in, without changing it.
isupper answers the question “is this string all capitals?” and islower
answers “is this string all small letters?”. Each one gives back a Boolean,
True or False:
print("HELLO".isupper())
print("hello".islower())
print("Hello".isupper())
The first two print True. The third prints False, because Hello has one
capital letter and four small ones, so it is not all capitals. Ask islower the
same question and it says False too: a string with a mix of both is neither.
Only the letters are looked at, so "HELLO 123!".isupper() is True. The
digits and punctuation have no case and do not count against it.
Make sure that you check for the following things:
len, upper, lower, isupper and islower are all spelled in small
letters.len goes around the string, as in len(name). The other four go
after it with a full stop, as in name.upper().upper(), lower(), isupper() and islower() end with a pair of empty
brackets.upper or lower is printed or stored in a variable. On its
own, name.upper() changes nothing.