How to Use _str_ in Python Quick Way to Use

Spread the love

Okay. To start with a simple example, I used the same Car class and added a. str () method to it. Dunder methods are those that begin with a double underscore (__) it’s just to dunder.

00:14 Many people dislike it when people refer to them as “magic ways,” because they aren’t designed to be magical in any manner.

They’re only supposed to be a Python convention, after all. And because the dunder, or double underscore (__), identifies them as a fundamental Python feature, classes aren’t allowed to define their own dunder methods because they might conflict with future Python features.

Use _str_ in Python

In this session, you’ll learn how to use __str__ to convert Python objects to strings in Python. You’ll also learn what the term __str__ is and how to utilize it in a Python class:

class Car:

def __init__(self, color, mileage):

self.color = color

self.mileage = mileage

def __str__(self):

return ‘a {self.color} car’.format(self=self)

So, similar to the. init () method, this is just a technique to namespace these things a little bit with a naming convention, keeping them apart a little bit.

But, you know, this is a complete diversion. So, in this case, I’ve created a. str () method. And basically what I have done here is that every time that method is invoked, it simply returns the Car’s.color and informs us it’s a Car.

So, just to give you an example, I’m going to make another the same vehicle And now, when I print this Car, I’ll be able to tell if it’

Isn’t it true that you obtain an entirely different result? So this time I receive a red automobile as a result of all of this. Instead of this ridiculous string containing the object address in memory, use the __str__() method. When I simply inspect this object, though, I receive the memory address from the previous result.

So, viewing the Car object yields the same result, but when I printed the Car object using the. str () method, I got this different result. If you wanted to force or force an object to become a string, here is how you would do it.

Use built-in str() method

You’d simply use the built-in str() method, which would then internally perform the appropriate thing and call the. str () method, returning the correct result.

All of these functions that deal with object text representations, such as the print() function, will now do so internally.

They’ll use the str() method on your behalf. The same might say about a format string, for example. If you do this, str () will call as well, and the result will return.

However, the important thing to remember is that adding a. str () function to your class by default will let you manage how your object is represented as a string. So this is the Pythonic way of doing it, which is kind of like the Holy Grail, right? Now, what you’re seeing is that I received the same result when I examined my car object in the console.

Therefore it appears that there are several methods for converting your objects to characters. The first is dunder, which we recently learned about. __str__().

Now for the next one, dunder.

__repr__(). Let’s discuss what. repr () performs and why it differs from. str ().

Leave a Comment