Fundamentals 1 min read

Understanding __str__ and __repr__ in Python's datetime.date

This short tutorial demonstrates how Python's datetime.date object displays differently with __str__ for a readable date string and __repr__ for an unambiguous constructor representation, showing the interpreter’s default use of __repr__ when inspecting objects.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Understanding __str__ and __repr__ in Python's datetime.date

Understanding __str__ and __repr__ in Python's datetime.date

<code>>> import datetime
>>> today = datetime.date.today()

# __str__ result should be readable:
>>> str(today)
'2017-02-02'

# __repr__ result should be explicit:
>>> repr(today)
'datetime.date(2017, 2, 2)'

# Python interpreter uses __repr__ to inspect objects:
>>> today
datetime.date(2017, 2, 2)</code>

*Disclaimer: This article is compiled from the web; copyright belongs to the original author. If any source information is incorrect or infringes rights, please contact us for removal or authorization.

Click Read Original to learn more.

pythonDateTimeprogramming fundamentals__str____repr__
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.