Python repr() Function
repr()is a built-in function in Python used to obtain the printable representation of an object.
repr()Returns the official string representation of an object, which can usually be used to recreate the object. It is different fromstr()in that,repr()it is more focused on debugging and development, whilestr()it is focused on user-friendliness.
Word Definition: reprYesrepresentationIt is the abbreviation of "representation".
Basic Syntax and Parameters
Syntax Format
repr(object)
Parameter Description
- Parameter object:
- Type: Any object
- Description: The object whose string representation is to be obtained.
Function Description
- Return value: Returns a string, usually a string that can represent the object.
- Special method: Objects can, by defining
__repr__a method, customize the return value.
Examples
Example 1: repr() vs str()
Examples
# Difference between strings
s = "hello"
print(f"str: '{str(s)}'") # Output: str: 'hello'
print(f"repr: '{repr(s)}'") # Output: repr: 'hello'
# Difference between datetime objects
from datetime import datetime
dt = datetime.now()
print(f"str: {str(dt)}")
print(f"repr: {repr(dt)}")
# Difference between custom classes
class Person:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Person: {self.name}"
def __repr__(self):
return f"Person(name='{self.name}')"
p = Person("Tom")
print(f"str: {str(p)}")
print(f"repr: {repr(p)}")
s = "hello"
print(f"str: '{str(s)}'") # Output: str: 'hello'
print(f"repr: '{repr(s)}'") # Output: repr: 'hello'
# Difference between datetime objects
from datetime import datetime
dt = datetime.now()
print(f"str: {str(dt)}")
print(f"repr: {repr(dt)}")
# Difference between custom classes
class Person:
def __init__(self, name):
self.name = name
def __str__(self):
return f"Person: {self.name}"
def __repr__(self):
return f"Person(name='{self.name}')"
p = Person("Tom")
print(f"str: {str(p)}")
print(f"repr: {repr(p)}")
Expected output:
str: 'hello' repr: 'hello' str: 2024-01-15 10:30:45.123456 repr: datetime.datetime(2024, 1, 15, 10, 30, 45, 123456) str: Person: Tom repr: Person(name='Tom')
Code analysis:
- For simple strings, both produce the same result.
- For datetime,
repr()returns an expression that can recreate the object. - Custom classes can respectively define
__str__and__repr__methods.
Example 2: Using repr() for Debugging
Examples
# Display hidden characters
s = "hello\nworld"
print(f"str: {str(s)}")
print(f"repr: {repr(s)}")
# Display quotes
text = "She said 'hello'"
print(f"str: {str(text)}")
print(f"repr: {repr(text)}")
# In an interactive environment
# Directly entering a variable name calls repr()
s = "hello\nworld"
print(f"str: {str(s)}")
print(f"repr: {repr(s)}")
# Display quotes
text = "She said 'hello'"
print(f"str: {str(text)}")
print(f"repr: {repr(text)}")
# In an interactive environment
# Directly entering a variable name calls repr()
Expected output:
str: hello world repr: 'hello\nworld' str: She said 'hello' repr: "She said 'hello'"
repr()It can display hidden characters (such as newline characters) and quotes, which is convenient for debugging.
Other Extensions
Python3 Built-in Functions