How To Print An Object As A String In Python

To print an object as a String in Python, you can print the desired attributes by the dot or the __repr__(), __str__() method. Follow the explanation below to understand better.

Print An Object As A String In Python

Using a dot

To print an object as a String in Python, you can use a dot and print the specified attributes of the object that you want.

Look at the example below.

# Create a class.
class Country:
    def __init__(self, name=str(), capital=str(), population=0, area=0):
        self.name = name
        self.capital = capital
        self.population = population
        self.area = area

# Create a new object.
country = Country('Vietnam', 'Ha Noi', 96, 333)

# Print attributes of an object with the dot.
print('Name: {}\nCapital: {}\nPopulation: {}\nArea: {}'.format(country.name,
                                                               country.capital, country.population, country.area))

Output

Name: Vietnam
Capital: Ha Noi
Population: 96
Area: 333

Using the __repr__() method

Besides using the dot, you can print an object as a String by overriding the __repr__() method. 

Look at the example below.

# Create a class.
class Country:
    def __init__(self, name=str(), capital=str(), population=0, area=0):
        self.name = name
        self.capital = capital
        self.population = population
        self.area = area

    # Override the __repr__() method.
    def __repr__(self):
        result_str = 'Name: {}\nCapital: {}\nPopulation: {}\nArea: {}'.format(
            self.name, self.capital, self.population, self.area)
        return result_str

# Create a new object.
country = Country('Vietnam', 'Ha Noi', 96, 333)

# Print attributes of an object with the repr() method.
print(repr(country))

Output

Name: Vietnam
Capital: Ha Noi
Population: 96
Area: 333

Using the __str__() method

In addition, you can override the __str__() method to print an object as a String in Python. After that, you can print an object by calling its name.

Look at the example below.

# Create a class.
class Country:
    def __init__(self, name=str(), capital=str(), population=0, area=0):
        self.name = name
        self.capital = capital
        self.population = population
        self.area = area

     # Override the __str__() method.
    def __str__(self):
        result_str = 'Name: {}\nCapital: {}\nPopulation: {}\nArea: {}'.format(self.name,
                                                                              self.capital, self.population, self.area)
        return result_str

# Create a new object.
country = Country('Vietnam', 'Ha Noi', 96, 333)

# Print attributes of an object with the __str__() method.
print(country)

Output

Name: Vietnam
Capital: Ha Noi
Population: 96
Area: 333

Summary

We have shown you how to print an object as a String in Python in 3 ways. Personally, you should override the __str__() method to achieve your goal because you can print an object by calling its name after overriding the __str__() method. Leave your comment below if you have any questions about this tutorial. Thanks!

Leave a Reply

Your email address will not be published. Required fields are marked *