Skip to main content

How to Solve "AttributeError: 'str' object has no attribute 'strftime'" in Python

The AttributeError: 'str' object has no attribute 'strftime' error in Python occurs when you attempt to use the strftime() method, which is designed for formatting datetime, date and time objects, on a string (str) object. Strings do not have a strftime() method.

This guide explains why this error occurs, provides solutions using datetime.strptime() to parse strings into datetime objects, and demonstrates correct usage.

Understanding the Error: strftime() Belongs to datetime

The strftime() method is used to format datetime, date and time objects into strings representing dates and times in a specific format. It is not a string method. Trying to call it on a string is the source of the AttributeError:

d = '2025-12-25 09:30:00.000123'

try:
print(d.strftime('%m,/%d/%Y')) # AttributeError: 'str' object has no attribute 'strftime'
except AttributeError as e:
print(e)

Output:

'str' object has no attribute 'strftime'
  • The strftime() is a method for datetime and date object, but it is being called on a string object.

Solutions

To fix this, you need a datetime object (or, in some cases, a date or time object, depending on what you're formatting).

Creating a datetime Object Directly

If you're working with a specific date and time, create a datetime object directly:

from datetime import datetime

d = datetime(2025, 12, 25, 9, 30, 0) # Year, month, day, hour, minute, second
print(d.strftime('%m/%d/%Y')) # Output: 12/25/2025
  • The datetime constructor is used to directly define a specific date and time.

Converting a String to a datetime Object with strptime()

If you have a string representation of a date and/or time, use datetime.strptime() to parse it into a datetime object:

from datetime import datetime

d = '2025-12-25 09:30:00.000123'
datetime_obj = datetime.strptime(d, '%Y-%m-%d %H:%M:%S.%f') # Parse the string
print(datetime_obj.strftime("%A, %d. %B %Y %I:%M%p")) # Format and print
# Output: Thursday, 25. December 2025 09:30AM
  • datetime.strptime(date_string, format): This is the crucial function.
    • date_string: The string you want to parse.
    • format: A string specifying the format of the date_string. This tells strptime how to interpret the different parts of your string. The format codes must match.
  • The strptime() transforms a string into a datetime object.

Formatting Dates and Times

Once you have a datetime object, you can use strftime() to format it into a string with your desired representation.

from datetime import datetime

today = datetime.today()
print(today) # Output: 2025-03-22 14:24:08.802823

todays_date = today.strftime('%Y-%m-%d')
print(todays_date) # Output: 2025-03-22

current_time = today.strftime('%H:%M:%S')
print(current_time) # Output: 14:24:08

Common Mistakes and Troubleshooting

  • Incorrect Variable Type: Always double-check the type of your variable using type(your_variable). If it's a string, you need to convert it to a datetime object before using strftime().

  • Mismatched Format Strings: Ensure that the format string you use with strptime() exactly matches the format of your date/time string. If there's a mismatch, you'll get a ValueError.

  • Using dir() to check string methods. Use the dir() function to check if a method is actually available in the object:

    my_string = 'tutorialreference.com'
    print(dir(my_string)) # Will not include strftime()
    # Output: ['__add__', ..., 'capitalize', ..., 'find',..., 'replace', 'split', ...]

Conclusion

The AttributeError: 'str' object has no attribute 'strftime' error occurs because strftime() is a method for datetime objects, not strings.

  • Use datetime.strptime() to parse a string into a datetime object before attempting to format it with strftime().
  • Always verify the type of your variable and ensure your format strings are correct.

By understanding this fundamental distinction, you'll avoid this common error and work with dates and times in Python effectively.