How to format a timedelta to a string with Python?

Sometimes, we want to format a timedelta to a string with Python.

In this article, we’ll look at how to format a timedelta to a string with Python.

How to format a timedelta to a string with Python?

To format a timedelta to a string with Python, we can call str.

For instance, we write

import datetime

start = datetime.datetime(2022, 2, 10, 14, 00)
end = datetime.datetime(2022, 2, 10, 16, 00)
delta = end - start

print str(delta)

to create a timedelta with

delta = end-start

Then we call str with delta to format timedelta delta into a string.

Conclusion

To format a timedelta to a string with Python, we can call str.