In This Article
The strptime() method in Python is used to parse a string representation of a date and time into a datetime
object. The name “strptime” stands for “string parse time,” indicating its purpose of converting a formatted string into a structured date and time object. This method is part of the datetime
module in Python’s standard library and is commonly used when working with dates and times.
Working of strptime() in Python
The strptime()
method takes two parameters: the string to be parsed and a format string that defines the expected format of the input string. The format string consists of various format codes, each representing a specific part of the date and time. These codes are similar to the ones used in the strftime()
method, but serve the opposite purpose of interpreting a string and extracting values from it.
Here’s a step-by-step explanation of how strptime()
works:
- Import the
datetime
module: Before usingstrptime()
, we need to import thedatetime
module from Python’s standard library. This module provides classes and functions for working with dates and times.
from datetime import datetime
- Define the input string and format: Next, we need to define the input string that represents the date and time we want to parse. Additionally, we need to specify the format string that matches the structure of the input string. The format string consists of format codes that represent the different components of the date and time.
input_string = "2023-06-28 15:30:45"
format_string = "%Y-%m-%d %H:%M:%S"
In this example, the input string "2023-06-28 15:30:45"
represents a date and time, and the format string "%Y-%m-%d %H:%M:%S"
defines the expected format.
The format codes used in the format string have the following meanings:
%Y
: Four-digit year (e.g., 2023)%m
: Zero-padded month (e.g., 06 for June)%d
: Zero-padded day of the month (e.g., 28)%H
: Zero-padded hour in 24-hour format (e.g., 15 for 3 PM)%M
: Zero-padded minute (e.g., 30)%S
: Zero-padded second (e.g., 45)
- Call
strptime()
with the input string and format: Once we have defined the input string and the format string, we can pass them as parameters to thestrptime()
method. Thestrptime()
method then processes the input string using the format string, extracts the values for the corresponding date and time components, and returns adatetime
object representing the parsed date and time.
parsed_datetime = datetime.strptime(input_string, format_string)
In the above example, the strptime()
method is called with the input_string
and format_string
, resulting in the parsed_datetime
object that holds the parsed date and time information.
- Use the parsed
datetime
object: Once the string has been successfully parsed bystrptime()
, we can work with the resultingdatetime
object. It allows us to perform various operations, such as date arithmetic, formatting, or comparison. We can output the parseddatetime
object, manipulate it, or use it as needed in our program.
print("Parsed DateTime:", parsed_datetime)
By specifying the format string that matches the structure of the input string, the strptime()
method is able to parse the string and extract the corresponding values for year, month, day, hour, minute, and second.
Note: If the input string does not adhere to the expected format specified in the format string, a ValueError
will be raised. Therefore, it’s crucial to ensure that the input string is correctly formatted according to the format string provided to strptime()
.
Example of Python strptime() method
from datetime import datetime
# Define the input string and format
input_string = "2023-06-28 15:30:45"
format_string = "%Y-%m-%d %H:%M:%S"
# Parse the input string using strptime()
parsed_datetime = datetime.strptime(input_string, format_string)
# Print the parsed datetime object
print("Parsed DateTime:", parsed_datetime)
In this example, we have an input string "2023-06-28 15:30:45"
representing a date and time. We define the expected format of the input string using the format string "%Y-%m-%d %H:%M:%S"
.
The format codes used in the format string have the same meanings as in the strftime()
method:
%Y
: Four-digit year (e.g., 2023)%m
: Zero-padded month (e.g., 06 for June)%d
: Zero-padded day of the month (e.g., 28)%H
: Zero-padded hour in 24-hour format (e.g., 15 for 3 PM)%M
: Zero-padded minute (e.g., 30)%S
: Zero-padded second (e.g., 45)
The output of the example would be something like:
Parsed DateTime: 2023-06-28 15:30:45
Once the string has been successfully parsed by strptime()
, we can work with the resulting datetime
object to perform various operations, such as date arithmetic, formatting, or comparison.
The strptime()
method provides a convenient way to convert formatted strings into datetime
objects, allowing us to work with date and time data in a structured and meaningful manner.