Python datetime from string

In this Python datetime tutorial, you will learn how to find a Python datetime from string.

# import datetime module
import datetime

# create a string
var_str = "21 April, 2020"

# print the string
print("var_str =", var_str)

# print the type of var_str
print("type of var_str =", type(var_str))

# convert string to datetime
var_date = datetime.datetime.strptime(var_str, "%d %B, %Y")

# print the var_date
print("var_date =", var_date)

# print the type of var_date
print("type of var_date =", type(var_date))

Output:

var_str = 21 April, 2020
type of var_str = <class 'str'>
var_date = 2020-04-21 00:00:00
type of var_date = <class 'datetime.datetime'>

Free resources to learn advanced skills: AiHints and CodeAllow