Articles
Screencasts
Exercises
Python
Tips
Tools
Python Pastebin
Online Python REPL
f-string formatter
strptime
undataclass
Python Glossary
Python Jumpstart
/
Sign Up
Sign In
String formatting format spec, conversions, and self-documenting expressions.
Click to Edit
""" String formatting format spec, conversions, and self-documenting expressions. https://www.pythonmorsels.com/string-formatting/ """ from datetime import datetime now = datetime.now() last_millennium = datetime(1999, 12, 31) print(f"Hello! It's {now:%B %d, %Y} at {now:%H:%M}") print(f"We're {(now - datetime(now.year, 1, 1)).days/365:.0%} through {now.year}") print(f"And we're {(now - last_millennium).days:,} days into this millennium") print(f"This object represents the current time: {now!r}") print(f"This gave us our year progress: {(now - datetime(now.year, 1, 1)).days/365 = }")
Copy Output
1759 views
Copy
Code copied
Run
in Browser
pym.dev/p/25aaf/
URL copied
Save Into New Paste
Need to share some Python code?
New Python snippet