<p>I need to parse <a href="https://www.rfc-editor.org/rfc/rfc3339" rel="noreferrer">RFC 3339</a> strings like <code>&quot;2008-09-03T20:56:35.450686Z&quot;</code> into Python's <code>datetime</code> type.</p>
<p>I have found <a href="https://docs.python.org/library/datetime.html#datetime.datetime.strptime" rel="noreferrer"><code>strptime</code></a> in the Python standard library, but it is not very convenient.</p>
<p>What is the best way to do this?</p>

What is the exact error you get? Is it like the following?
```
```
>>> datetime.datetime.strptime("2008-08-12T12:20:30.656234Z", "%Y-%m-%dT%H:%M:%S.Z")
ValueError: time data did not match format:  data=2008-08-12T12:20:30.656234Z  fmt=%Y-%m-%dT%H:%M:%S.Z
```
```
If yes, you can split your input string on ".", and then add the microseconds to the datetime you got.
Try this:
```
```
>>> def gt(dt_str):
        dt, _, us= dt_str.partition(".")
        dt= datetime.datetime.strptime(dt, "%Y-%m-%dT%H:%M:%S")
        us= int(us.rstrip("Z"), 10)
        return dt + datetime.timedelta(microseconds=us)

>>> gt("2008-08-12T12:20:30.656234Z")
datetime.datetime(2008, 8, 12, 12, 20, 30, 656234)
```
```