This is the difference between a function and a method. If you are only just learning the basics, maybe simply accept that this difference exists, and that you will eventually understand it.
Still here? It's not even hard, actually. In object-oriented programming, methods are preferred over functions for many things, because that means one type of object can override its version of the method without affecting the rest of the system.
For example, let's pretend you had a new kind of string where accented characters should lose their accent when you call .upper()
. Instances of this type can subclass str
and behave exactly the same in every other aspect, basically for free; all they need to redefine is the upper
method (and even then, probably call the method of the base class and only change the logic when you handle an accented lowercase character). And software which expects to work on strings will just continue to work and not even know the difference if you pass in an object of this new type where a standard str
is expected.
A design principle in Python is that everything is an object. This means you can create your own replacements even for basic fundamental objects like object
, class
, and type
, i.e. extend or override the basic language for your application or platform.
In fact, this happened in Python 2 when unicode
strings were introduced to the language. A lot of application software continued to work exactly as before, but now with unicode
instances where previously the code had been written to handle str
instances. (This difference no longer exists in Python 3; or rather, the type which was called str
and was used almost everywhere is now called bytes
and is only used when you specifically want to handle data which is not text.)
Going back to our new upper
method, think about the opposite case; if upper
was just a function in the standard library, how would you even think about modifying software which needs upper
to behave differently? What if tomorrow your boss wants you to do the same for lower
? It would be a huge undertaking, and the changes you would have to make all over the code base would easily tend towards a spaghetti structure, as well as probably introduce subtle new bugs.
This is one of the cornerstones of object-oriented programming, but it probably only really makes ense when you learn the other two or three principles in a more structured introduction. For now, perhaps the quick and dirty summary is "methods make the implementation modular and extensible."