How To Resolve “AttributeError: ‘str’ Object Has No Attribute ‘strftime'” In Python

AttributeError: ‘str’ object has no attribute ‘strftime’ in Python

The error “AttributeError: ‘str’ object has no attribute ‘strftime'” occurs when you access the strftime() function on a string. To fix the error, follow the article to know specific steps.

What causes the AttributeError: ‘str’ object has no attribute ‘strftime’ in Python?

The error occurs when you access the strftime() function on a string. Note that the strftime() function appears on the datetime object. 

The strftime() function returns a string representing the day, month, year, and time values ​​through the date, time, and datetime objects in the ‘datetime’ module.

How does the strftime() function work? In the program, the format codes are %Y, %m, %d,… The strftime() function takes the format code as an argument and returns a string of days, months, and years based on that format code. 

Standard format codes like:

  •  %Y: year value
  • %m: month value
  •  %d: day value
  •  %H: hour value
  • %M: minutes value
  • %S: second value

Example: 

myDate = '2022-09-08 23:55:59.342380'
formatDate = myDate.strftime(myDate, "%d/%m/%Y")

print(formatDate)

Output:

Traceback (most recent call last):
  File "code.py", line 2, in <module>
    formatDate = myDate.strftime(myDate, "%d/%m/%Y")
AttributeError: 'str' object has no attribute 'strftime'

How to solve this error?

Use strptime() function

You can use the strptime() function to convert the string to a datetime object. Python’s strptime() function creates a datetime object from a given string. However, not any string can be passed to the function. The string here must satisfy a particular format to return the result.

Syntax:

Strptime(obj,format_code)

Parameters:

  • obj: string you want to convert to datetime.
  • format_code: format code.

The strptime() returns the datetime object corresponding to the format code.

Example:

  • Import module datetime.
  • Create a string.
  • Use the strptime() function to convert the string to a datetime object.
  • When the strftime() function is called, there will be no errors.

Example:

from datetime import datetime
myDate = '2022-09-08 23:55:59.342380'

# Use the strptime() function to convert the string to a datetime object
dateObj = datetime.strptime(myDate, '%Y-%m-%d %H:%M:%S.%f')

# Use strftime() to format Date Object
newDate = dateObj.strftime("%d/%m/%Y")

print(newDate)

Output:

08/09/2022

Create datetime class objects that are integers

Example:

  • Import module datetime.
  • Declare a variable containing datetime objects that are integers.
  • Use the strftime() function to print the day, month, and year.
from datetime import datetime

# Create datetime class objects
myDate = datetime(2022, 8, 9)

newDate = myDate .strftime('%d/%m/%Y')
print(newDate)

Output:

09/08/2022

If you always format the datetime object as integers, then, indeed, this error will never occur.

Summary

If you have any questions about the error “AttributeError: ‘str’ object has no attribute ‘strftime’” in Python, please leave a comment below. I will answer your questions. Thanks for reading!

Maybe you are interested:

Leave a Reply

Your email address will not be published. Required fields are marked *