1

I am looking to remove the space separating the date and the time from a python datetime object. I am using strptime "%Y-%m-%dT%H:%M:%S.%f", so I do not know why there is a space included to begin with.

Code:

import datetime
start_timestamp = "2022-11-23T10:08:00.000"
date_time_start = datetime.datetime.strptime(start_timestamp, "%Y-%m-%dT%H:%M:%S.%f")
print(date_time_start)

Output: 2022-11-23 10:08:00

Desired output: 2022-11-23_10:08:00

1
  • 2
    You asked why the space is printed. The strptime function returns a datetime, not a string. When you print it, a string representation of the datetime value is printed, and that, by default, contains a space, which is a completely sensible default. Commented Jan 5, 2023 at 22:31

1 Answer 1

2

Use isoformat with custom separator:

>>> date_time_start.isoformat(sep="_")
'2022-11-23_10:08:00'
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.