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

Linda Hamilton
Release: 2024-11-24 11:03:15
Original
919 people have browsed it

Why we use string.upper() instead of upper(name) in 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
Copy after login

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
Copy after login

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 .

The above is the detailed content of Why we use string.upper() instead of upper(name) in Python ?. For more information, please follow other related articles on the PHP Chinese website!

source:dev.to
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template