🎓 Top 15 Udemy Courses (80-90% Discount): My Udemy Courses - Ramesh Fadatare — All my Udemy courses are real-time and project oriented courses.
▶️ Subscribe to My YouTube Channel (178K+ subscribers): Java Guides on YouTube
▶️ For AI, ChatGPT, Web, Tech, and Generative AI, subscribe to another channel: Ramesh Fadatare on YouTube
1. Introduction
In Python, str and repr are two methods that are used to represent objects as strings. The str function is meant to return representations of objects that are readable and appealing to human eyes. In contrast, repr is used to produce a string representation of an object that is more technical and is often valid Python code that can be used to recreate the object.
2. Key Points
1. Audience: str is for a human-readable representation, repr for a developer-centric and detailed representation.
2. Purpose: str emphasizes readability, repr emphasizes accuracy, and sometimes includes more detail.
3. Return Value: str might be less detailed, repr aims to return a string that, when passed to eval(), could recreate the object.
4. Default Implementation: If not defined, str uses repr, while repr needs explicit implementation.
3. Differences
| Aspect | str | repr |
|---|---|---|
| Audience | Human-readable | Developer-centric |
| Purpose | Emphasizes readability | Emphasizes accuracy |
| Return Value | Maybe less detailed | Can recreate the object |
| Default Implementation | Uses repr if not defined | Needs explicit implementation |
4. Example
class MyObject:
def __init__(self, name):
self.name = name
def __str__(self):
return f'MyObject named {self.name}'
def __repr__(self):
return f'MyObject({self.name!r})'
obj = MyObject('Python')
# Using str
str_output = str(obj)
# Using repr
repr_output = repr(obj)
Output:
str Output:
MyObject named Python
repr Output:
MyObject('Python')
Explanation:
1. The str output provides a readable description, showing just the name of the object.
2. The repr output looks like the Python code to recreate the object, showing the class name and the name in a format that could be used to reconstruct the object.
5. When to use?
- Use str when you need a readable representation of an object, like in print statements or in user interfaces.
- Use repr when you need an unambiguous representation of an object that is more for debugging and development, potentially including more detailed information about the object.
Related Python Posts:
Difference Between Local and Global Variables in Python
Difference Between List and Tuple in Python
Difference Between Array and List in Python
Difference Between List and Dictionary in Python
Difference Between List, Tuple, Set and Dictionary in Python
Difference Between a Set and Dictionary in Python
Difference between for loop and while loop in Python
Difference Between pass and continue in Python
Difference Between List append and extend in Python
Difference Between == and is operator in Python
Difference Between Deep and Shallow Copy in Python
Class Method vs Static Method in Python
Class Method vs Instance Method in Python
Difference Between List and Set in Python
Difference Between Generator and Iterator in Python
Difference Between str and repr in Python
Method Overloading vs Overriding in Python
Difference Between Dictionary and Tuple in Python
Difference Between Dictionary and Object in Python
Difference Between Mutable and Immutable in Python
Difference Between Interface and Abstract Class in Python
Difference Between Python Script and Module
Difference Between for Loop and Iterator in Python
My Top and Bestseller Udemy Courses. The sale is going on with a 70 - 80% discount. The discount coupon has been added to each course below:
Build REST APIs with Spring Boot 4, Spring Security 7, and JWT
[NEW] Learn Apache Maven with IntelliJ IDEA and Java 25
ChatGPT + Generative AI + Prompt Engineering for Beginners
Spring 7 and Spring Boot 4 for Beginners (Includes 8 Projects)
Available in Udemy for Business
Building Real-Time REST APIs with Spring Boot - Blog App
Available in Udemy for Business
Building Microservices with Spring Boot and Spring Cloud
Available in Udemy for Business
Java Full-Stack Developer Course with Spring Boot and React JS
Available in Udemy for Business
Build 5 Spring Boot Projects with Java: Line-by-Line Coding
Testing Spring Boot Application with JUnit and Mockito
Available in Udemy for Business
Spring Boot Thymeleaf Real-Time Web Application - Blog App
Available in Udemy for Business
Master Spring Data JPA with Hibernate
Available in Udemy for Business
Spring Boot + Apache Kafka Course - The Practical Guide
Available in Udemy for Business
Comments
Post a Comment
Leave Comment