Skip to main content

5.8) String Methods


Methods are functions that perform tasks specifically on one type of object, such as strings. Two useful string methods are lower() and upper().
Calling a method is similar to calling a function, except that you call a method on an object, using the syntax:
object.method(argument)
Notice the dot linking the object name and the method name.
For example:

>>> my_string='Hello, World!'
>>> my_string.lower()
'hello, world!'
>>> my_string.upper()
'HELLO, WORLD!'

The methods lower() and upper() return a new string which has the same characters as the initial string but with all letters in lowercase or uppercase respectively. Notice that these functions do not require any input argument, but to call a function or method, you need to follow it with round brackets even if they’re empty.
More string methods: http://docs.Python.org/2/library/stdtypes.html#string-methods