To return the day of the week from a string, you have to import the datetime module and run this code:
It will return the day of the week:
Let’s analyze the code:
The strptime function creates a datetime object from the string. The string has to be in a certain format.
The next part of the code returns a string representation of a date. In this case, it’s a day.
You can also use the lowercase letter, like this:
This time it’s going to return day in the abbreviated form.
The weekday function
The next way, you can return the day of the week is the weekday function. It will use the same module: datetime. For this method, you have to use a different approach.
First, we are going to return the number of the day of the week. in our case, it’s 5.
The result is the sith position of the tuple:
It’s especially useful if you want to use custom names for the days or use names in different languages.