How to Convert Weekday Number to Name in Python
When working with dates in Python, you often deal with numeric representations of weekdays (e.g., 0 for Monday, 6 for Sunday). However, for user interfaces and reports, you need to convert these integers into human-readable strings like "Monday" or "Tue".
This guide explores the most efficient ways to convert weekday numbers to names using the calendar module, datetime formatting, and custom mappings for specific requirements.
Understanding Python Weekday Numbering
Before converting, it is critical to know which number represents which day. Python's standard datetime.date.weekday() method follows this convention:
| Number | Day |
|---|---|
| 0 | Monday |
| 1 | Tuesday |
| ... | ... |
| 6 | Sunday |
Some other systems (like Cron or certain SQL flavors) might treat 0 as Sunday. Always verify the source of your integer.
Method 1: Using the calendar Module (Standard)
The built-in calendar module provides localized arrays of day names. This is the standard way to get day names without manually typing them out.
You can access full names via day_name and abbreviated names via day_abbr.
import calendar
weekday_index = 3 # 0=Mon, 3=Thu
# ✅ Correct: Accessing by index
full_name = calendar.day_name[weekday_index]
short_name = calendar.day_abbr[weekday_index]
print(f"Index {weekday_index}: {full_name} ({short_name})")
Output:
Index 3: Thursday (Thu)
Method 2: Using datetime.strftime() (For Date Objects)
If you have a datetime or date object (rather than just an integer), you do not need to extract the number first. You can format the date directly into a string using format codes.
%A: Full weekday name (e.g., Wednesday)%a: Abbreviated weekday name (e.g., Wed)
from datetime import datetime
current_date = datetime.now()
# ✅ Correct: Format directly from the date object
full_day = current_date.strftime("%A")
short_day = current_date.strftime("%a")
print(f"Today is: {full_day}")
print(f"Short: {short_day}")
Output:
Today is: Wednesday
Short: Wed
Method 3: Using Custom Lists (Fast & Flexible)
If you need specific labels (e.g., different languages, specific casing, or "Weekends"), creating a simple list is often the fastest and most readable solution. This avoids importing modules for simple tasks.
# Define your own mapping
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
day_number = 4 # Friday
# ✅ Correct: Access by index
print(f"Day is: {days[day_number]}")
Output:
Day is: Fri
Common Pitfall: Monday (0) vs. Sunday (0)
A common bug occurs when the integer source uses a different starting day than Python. For example, if your data comes from a system where 0 is Sunday, but you use Python's calendar (where 0 is Monday), the days will be shifted by one.
import calendar
# Data source: 0=Sunday, 1=Monday, ... 6=Saturday
external_system_day = 0
# ⛔️ Incorrect: calendar module expects 0=Monday
wrong_day = calendar.day_name[external_system_day]
print(f"System says Sunday (0), Python converts to: {wrong_day}")
# ✅ Correct: Adjust the index or use a custom list matching the source
# If source is 0=Sun, 1=Mon... and Python is 0=Mon, 1=Tue...
# We can shift: (day - 1) % 7 maps Sunday(0) -> -1 -> 6 (Sunday in Python)
python_compatible_index = (external_system_day - 1) % 7
correct_day = calendar.day_name[python_compatible_index]
print(f"Correctly converted: {correct_day}")
Output:
System says Sunday (0), Python converts to: Monday
Correctly converted: Sunday
Always check if your input integer comes from datetime.weekday() (0=Mon) or datetime.isoweekday() (1=Mon, 7=Sun). calendar.day_name expects a 0-based index starting on Monday.
Conclusion
To convert a weekday number to a name in Python:
- Use
calendar.day_name[i]if you have an integer (0=Monday) and want standard English names. - Use
date.strftime("%A")if you already have a datetime object. - Use a Custom List if your input integer follows a different convention (e.g., 0=Sunday) or you need custom labels.