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 |
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
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()
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: WedMethod 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.
days = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
day_number = 4 # Friday
print(f"Day is: {days[day_number]}")Output:
Day is: FriCommon 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
external_system_day = 0
wrong_day = calendar.day_name[external_system_day]
print(f"System says Sunday (0), Python converts to: {wrong_day}")
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: SundayConclusion
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.