The Python calendar.firstweekday() function is used to get the current setting for the first day of the week in the calendar module.
By default, the first weekday is set to Monday (0), but it can be changed using calendar.setfirstweekday() function.
SyntaxFollowing is the syntax of the Python calendar.firstweekday() function −
calendar.firstweekday()Parameters
This function does not accept any parameters.
Return ValueThis function returns an integer representing the first weekday. The mapping is −
By default, the first weekday is Monday (0) −
import calendar # Get the default first weekday first_day = calendar.firstweekday() print("Default first weekday:", first_day)
We get the output as shown below −
Default first weekday: 0Example: Changing and Retrieving the First Weekday
We can use the calendar.setfirstweekday() function to change the first weekday before calling firstweekday() function −
import calendar # Set first weekday to Sunday calendar.setfirstweekday(calendar.SUNDAY) # Get the updated first weekday first_day = calendar.firstweekday() print("Updated first weekday:", first_day)
Following is the output of the above code −
Updated first weekday: 6Example: Using First Weekday in Month Calendar
Changing the first weekday affects how the month calendar is displayed −
import calendar # Set first weekday to Wednesday calendar.setfirstweekday(calendar.WEDNESDAY) # Print the month calendar print(calendar.month(2025, 3))
Following is the output obtained −
March 2025 We Th Fr Sa Su Mo Tu 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31Example: Resetting to Default
To reset the first weekday back to Monday, we explicitly set it again −
import calendar # Reset to default (Monday) calendar.setfirstweekday(calendar.MONDAY) # Verify the change print("Reset first weekday:", calendar.firstweekday())
The result produced is as shown below −
Reset first weekday: 0
python_date_time.htm
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4