The strptime stands for string parse time, and this function is part of the datetime module and is used for parsing strings representing time according to a specified format.
Here is the basic usage of the strptime() function with the date
Program
Output:
2022-01-18 00:00:00
Explanation
Here, we specified the date and then formatted the string into the datetime object.
Here is an example of using the strptime to format the date and time
Program
Output:
2022-01-18 15:30:45
Explanation
The string is parsed using the strptime() method with a format string of "%Y-%m-%d %H:%M:%S" for both date and time components.
The example program handles the AM/PM information by converting the time into 24-hour format.
Program
Output:
2022-01-18 15:30:45
Explanation
This example demonstrates how to parse a string with AM/PM information. The format string "%Y-%m-%d %I:%M:%S %p" matches the structure of the input string.
Output:
2024-01-18 12:30:00 2023-02-20 08:45:00
Explanation
Here is a simple example to demonstrate the working of the strptime() function
Program
Output:
2024-01-18 12:30:45 <class 'datetime.datetime'>
Explanation
We have used the strptime() function to convert the string date to the original datetime object, specifying the particular format.
Let us consider the program to consider the code to handle a variety of date string formats.
Program
Output:
2024-01-18 00:00:00 2024-01-18 00:00:00 2024-01-18 00:00:00 2024-01-18 12:30:00 2024-01-18 12:30:00
In conclusion, strptime() is a powerful method for converting date and time strings into datetime objects in Python. It provides flexibility through the format string, allowing you to parse a various date and time representations.
We request you to subscribe our newsletter for upcoming updates.

We deliver comprehensive tutorials, interview question-answers, MCQs, study materials on leading programming languages and web technologies like Data Science, MEAN/MERN full stack development, Python, Java, C++, C, HTML, React, Angular, PHP and much more to support your learning and career growth.
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India