String Functions and Methods in Python
We got to know variables in the last chapter. A value can therefore be stored in a variable and displayed again.
The stored content of a variable is displayed using the print() function.
website = 'StackHowTo.com' print(website)
Functions with variables
We pass values (and statements) to a function after the function name between parenthesis. Our function “print(website)” gets the variable in the parenthesis and thus knows what is to be printed on the screen.
However, it is not always necessary to pass further values and statements to a function in the parenthesis. If print() is simply called with an empty bracket, then we get an empty line as a result.
Let’s look at another function besides print(). The len(x) function is used to determine the length of a string. The information we get here is the number of characters it contains. Len is the abbreviation of the English word “length”. However, we have to do something with this information. Let us output these:
website = 'StackHowTo.com' print(len(website))
Functions can therefore be used within other functions.
Functions are independent. We can not only use the functions for strings!
Functions are easy to understand so far. But what are methods and what can we do with variables with them?
Methods for variables / strings
Let’s change our perspective. If we look at our variable as an object, then this object can offer various possibilities. I am consciously writing possibilities here. Why? If we look at it from an object-oriented perspective, we are already fully into object-oriented programming (OOP). This will be discussed in detail later. It is fundamental that the dot notation is used in OOP. We have our object that comes first and we want to query something for this object or carry out a defined action: the action is mentioned via a point directly behind it.
object-name.action
For example, we could get our text stored in the variable entirely in lower case.
website = 'StackHowTo.com' website.lower()
Output:
stackhowto.com
Anyone who thinks that this feels like a function that simply converts everything into lowercase letters is not really wrong. It’s just a different view – that is, the object-oriented view. And so the object-oriented notation becomes necessary.
The real art lies in knowing which objects offer which possibilities (i.e. methods).
We can request this help for our strings (which are our variables) using the help (str) statement.
>>> help(str)
Difference Between Functions and Methods
Functions can be called directly by their name – methods, on the other hand, always require their object.
- Functions: functionname()
- Methods: object.method()
Functions are independent. Any function can be given to a function, which can then be used for further work. Methods are fixed – i.e. each object has certain options (i.e. methods). The trick is just to know what methods are already “standard”.
Tip for beginners
In the following subchapter, we will look at variables, which methods exist, and how they can be used. As a beginner, you can skim the subchapters, but it will be better to have a closer look at the possibilities here later. So before you get overwhelmed by many small possibilities, please get the basic overview first and continue in the next chapter “Operators for strings” and look at these subchapters later!