Game Development Reference
In-Depth Information
9 - Hangman
>>> 'Hello world'.upper()
'HELLO WORLD! '
>>>
Because the
upper()
method returns a string, you can call a method on
that
string as
well. Try typing
'Hello world!'.upper().lower()
into the shell:
>>> 'Hello world'.upper().lower()
'hello world!'
>>>
'Hello world!'.upper()
evaluates to the string
'HELLO WORLD!'
, and then
we call that string's
lower()
method. This returns the string
'hello world!'
, which
is the final value in the evaluation. The order is important.
'Hello world!'.lower
().upper()
is not the same as
'Hello world!'.upper().lower()
:
>>> 'Hello world'.lower().upper()
'HELLO WORLD!'
>>>
Remember, if a string is stored in a variable, you can call a string method on that
variable. Look at this example:
>>> fizz = 'Hello world'
>>> fizz.upper()
'HELLO WORLD'
>>>
The
reverse()
and
append()
List Methods
The list data type also has methods. The
reverse()
method will reverse the order of
the items in the list. Try entering
spam = [1, 2, 3, 4, 5, 6, 'meow',
'woof']
, and then
spam.reverse()
to reverse the list. Then enter
spam
to view the
contents of the variable.
>>> spam = [1, 2, 3, 4, 5, 6, 'meow', 'woof']
>>> spam.reverse()
>>> spam
['woof', 'meow', 6, 5, 4, 3, 2, 1]