🚀 LEVEL UP TO SENIOR:Unlock 500+ Advanced Practical Challenges & Exercises.
🎓 COURSERA PARTNER:Earn professional Google, Meta, and IBM certificates to supercharge your resume.
REFERENCEpython

python Documentation

LOADING ENGINE...

str()

AI & DATA SCIENCE // str

str() converts any object into its human-readable string representation by calling the object's __str__() method.

Syntax

str(object='')

Deep Dive Course

Every object in Python can be turned into a string: str() calls the object's __str__() method if it defines one, and falls back to __repr__() otherwise. This is exactly what print() does internally to each of its arguments, which is why print(42) and print(str(42)) produce identical output. You can customize how your own classes are displayed by overriding __str__().

1Understanding str()

Every object in Python can be turned into a string: str() calls the object's __str__() method if it defines one, and falls back to __repr__() otherwise. This is exactly what print() does internally to each of its arguments, which is why print(42) and print(str(42)) produce identical output. You can customize how your own classes are displayed by overriding __str__().

💡

__str__() is for a readable display shown to end users; __repr__() is for an unambiguous, debugging-oriented representation shown in the REPL and inside containers like lists — define both when writing a class meant to be printed.

editor.html
print(str(42))
print(str(3.14))
print(str([1, 2, 3]))
localhost:3000

2Practical Example

Here is a real-world application of str() showing how it is used in production Python code.

editor.html
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __str__(self):
        return f"({self.x}, {self.y})"

print(str(Point(3, 4)))
localhost:3000

3Best Practices

Follow these guidelines when working with str():

1. Override __str__() on custom classes so printing an instance shows something meaningful instead of a default memory-address representation

2. Use f-strings instead of manual str() plus concatenation when building messages

3. Remember str(None) is the text 'None', not an empty string

⚠️

Tip: __str__() is for a readable display shown to end users; __repr__() is for an unambiguous, debugging-oriented representation shown in the REPL and inside containers like lists — define both when writing a class meant to be printed.

editor.html
print(str(42))
print(str(3.14))
print(str([1, 2, 3]))
localhost:3000

Examples

Example 01Basic Usage
print(str(42))
print(str(3.14))
print(str([1, 2, 3]))
Example 02Advanced Example
class Point:
    def __init__(self, x, y):
        self.x, self.y = x, y
    def __str__(self):
        return f"({self.x}, {self.y})"

print(str(Point(3, 4)))

Best Practices

  • Override __str__() on custom classes so printing an instance shows something meaningful instead of a default memory-address representation
  • Use f-strings instead of manual str() plus concatenation when building messages
  • Remember str(None) is the text 'None', not an empty string

Interview Question

What's the difference between __str__ and __repr__, and which one does str() call?

Hint: One is for users, one is for developers.

str(obj) calls __str__(), meant to produce a readable, user-facing string. __repr__() is meant to produce an unambiguous representation useful for debugging, ideally one that could recreate the object. If a class defines only __repr__(), str() falls back to it automatically, since the default __str__ implementation on object just calls __repr__().

Exercises

MediumPractice using str() in a real scenario.
View Solution
print(str(42))
print(str(3.14))
print(str([1, 2, 3]))

Frequently Asked Questions

What's the difference between __str__ and __repr__, and which one does str() call?

str(obj) calls __str__(), meant to produce a readable, user-facing string. __repr__() is meant to produce an unambiguous representation useful for debugging, ideally one that could recreate the object. If a class defines only __repr__(), str() falls back to it automatically, since the default __str__ implementation on object just calls __repr__().

Related Functions

repr()format()f-strings