DEV Community

18#Aman Kumar Verma
18#Aman Kumar Verma

Posted on

Why we use string.upper() instead of upper(name) in Python ?

python

This question click in my mind when i am learning new programming languages or scripting languages. I know mostly people know about this why we use string.upper() instead of upper(string). If any one have completed the full concepts of OOPs in any other languages then they also know reason.

So before deep diving into this i recommend you to clear the OOPS's(Object Oriented Programming) concepts.

As we know in OOPs we have class and object and class contains methods or functions so if we want to use the methods of that class first we have to create object of that class and through object we can call the function of that class.
Just like that .upper() is a method of str class and and when we used on any variables that holding text or string. Its means that we are calling the .upper() method of str class on the given string that looks like this

name = "aditya"
print(name.upper()) # ADITYA
Enter fullscreen mode Exit fullscreen mode

or Hypothetically we can assume that name is object and upper is a method.

class str:
    def __init__(self,value):
          self.value = value

    def upper(self):
          return self.value.upper()

name = str("aditya")
print(name.upper()) # ADITYA
Enter fullscreen mode Exit fullscreen mode

Another things which we know is that for finding length of any string we write len(string) because we have to find length of every datatypes like list, dict, tuples etc. so it is used globally for all as compare to .upper() follow oops principles as it is a method of str class. whereas len() is just a function.

I hope you like this Blog .

Top comments (0)