time input in python code example

Example 1: simple time in python

t = time.localtime()            # Gets the local time
current_time = time.strftime("%H:%M", t)          # Gets the time in the desired format
current_time = "The time is " + current_time

Example 2: how to take input in python

# Taking string input
a = input("Enter a string: ")
print("String is: ", a)

# Taking integer input
b = int(input("Enter an integer: "))
print("Integer is: ", b)

# Taking float input
c = float(input("Enter a float value: "))
print("Float value is: ", c)

Example 3: python run things at certain datetimes

import datetime as DT
import time

while True:
    now = DT.datetime.now()
    target = DT.datetime.combine(DT.date.today(), DT.time(hour=8))
    if target < now:
        target += DT.timedelta(days=1)

    time.sleep((target-now).total_seconds())
    # do something