4) String Methods
We previously saw that each turtle instance has its own attributes and a number of methods that can be applied to the instance. For example, we wrote tess.right(90)
when we wanted the turtle object tess
to perform the right
method to turn to the right 90 degrees. The “dot notation” is the way we connect the name of an object to the name of a method it can perform.
Strings are also objects. Each string instance has its own attributes and methods. The most important attribute of the string is the collection of characters. There are a wide variety of methods. Try the following program.
In this example, upper
is a method that can be invoked on any string object to create a new string in which all the characters are in uppercase. lower
works in a similar fashion changing all characters in the string to lowercase. (The original string ss
remains unchanged. A new string tt
is created.)
In addition to upper
and lower
, the following table provides a summary of some other useful string methods. There are a few activecode examples that follow so that you can try them out.
You should experiment with these methods so that you understand what they do. Note once again that the methods that return strings do not change the original. You can also consult the Python documentation for strings.