Python strptime()
The strptime()
method creates a datetime
object from a given string.
The string needs to be in a specific format to create a datetime
object from it.
Example: string to datetime object
In the following example, we will create a datetime object from a given string having a specific format.
from datetime import datetime
date_string = "19 July, 2017"
print("date_string =", date_string)
print("type od date_string =", type(date_string))
date_object = datetime.strptime(date_string, "%d %B, %Y")
print("date_object =", date_object)
print("type od date_object =", type(date_object))
After running the above program, the output will be as follows:
date_string = 19 July, 2017
type od date_string = <class 'str'>
date_object = 2017-07-19 00:00:00
type od date_object = <class 'datetime.datetime'>
How strptime() works?
The strptime()
method accepts two arguments:
- string (to be converted to datetime)
- format code of the given string
The strptime()
method returns its equivalent datetime
object using the string and format code.
In the above example:

The above format codes:
%d
- It represents the day of the month. Example: 01, 02, ..., 31%B
- It represents a month's name in full. Example: January, February, etc.%Y
- It represents a year in four digits. Example: 2017, 2018, etc.
Example: creating a datetime object from a string
In the following example, we will create a datetime
object from a string using two different formats.
from datetime import datetime
dt_str = "10/05/2017 10:18:28"
# Considering date is in mm/dd/yyyy format
dt_obj1 = datetime.strptime(dt_str, "%m/%d/%Y %H:%M:%S")
print(dt_obj1)
# Considering date is in dd/mm/yyyy format
dt_obj2 = datetime.strptime(dt_str, "%d/%m/%Y %H:%M:%S")
print(dt_obj2)
After executing the above program, the output will be:
2017-10-05 10:18:28
2017-05-10 10:18:28
Format Code List
The following table shows all the format codes that we can pass to the strptime()
method.
Directive | Meaning | Example |
---|---|---|
%a |
Abbreviated weekday name. | Mon, Tue, ... |
%A |
Full weekday name. | Monday, Tuesday, ... |
%w |
Weekday as a decimal number. | 0, 1, ..., 6 |
%d |
Day of the month as a zero-padded decimal. | 01, 02, ..., 31 |
%-d |
Day of the month as a decimal number. | 1, 2, ..., 31 |
%b |
Abbreviated month name. | Jan, Feb, ..., Dec |
%B |
Full month name. | January, February, ..., December |
%m |
Month as a zero-padded decimal number. | 01, 02, ..., 12 |
%-m |
Month as a decimal number. | 1, 2, ..., 12 |
%y |
Year without century as a zero-padded decimal number. | 00, 01, ..., 99 |
%-y |
Year without century as decimal number. | 0, 1, ..., 99 |
%Y |
Year with century as a decimal number. | 2013, 2019, etc |
%H |
Hour (24-hour clock) as a zero-padded decimal number. | 00, 01, ..., 23 |
%-H |
Hour (12-hour clock) as a decimal number. | 0, 1, ..., 23 |
%I |
Hour (12-hour clock) as a zero-padded decimal number. | 01, 02, ..., 12 |
%-I |
Hour (12-hour clock) as a decimal number. | 1, 2, ..., 12 |
%p |
Locale's AM or PM. | AM, PM |
%M |
Minute as a zero-padded decimal number. | 00, 01, ..., 59 |
%-M |
Minute as a decimal number. | 0, 1, ..., 59 |
%S |
Second as a zero-padded decimal number. | 00, 01, ..., 59 |
%-S |
Second as a decimal number. | 0, 1, ..., 59 |
%f |
Microsecond as a decimal number, zero-padded on the left. | 000000 - 999999 |
%z |
UTC offset in the form +HHMM or -HHMM. | |
%Z |
Time zone name. | |
%j |
Day of the year as a zero-padded decimal number. | 001, 002, ..., 366 |
%-j |
Day of the year as a decimal number. | 1, 2, ..., 366 |
%U |
Week number of the year (Sunday as the first day of the week). All days in a new year preceding the first Sunday are considered to be in week 0. | 00, 01, ..., 53 |
%W |
Week number of the year (Monday as the first day of the week). All days in a new year preceding the first Monday are considered to be in week 0. | 00, 01, ..., 53 |
%c |
Locale's appropriate date and time representation. | Tue Jan 26 10:19:08 2017 |
%x |
Locale's appropriate date representation. | 01/26/17 |
%X |
Locale's appropriate time representation. | 10:19:08 |
%% |
A literal '%' character. | % |
ValueError in strptime()
The striptime()
will throw a ValueError
exception when the string (first argument) does not match the format code (second argument).
from datetime import datetime
d_str = "05/26/2017"
d_obj = datetime.strptime(d_str, "%m %d %Y")
print("d_obj =", d_obj)
After running the above program, we will get an error.
ValueError: time data '05/26/2017' does not match format '%m %d %Y'
We also recommend to check Python strftime(). The strftime()
method is the opposite of the strptime()
, it creates a string from a datetime
object.