I need to access the first 10 characters of a key-value in dictionary. My dictionary looks like this:
JavaScript
x
2
1
data = {"id": "bob", "start_date": "2020-01-01 00:00:00"}
2
I have tried:
JavaScript
1
3
1
for key, value in data.items():
2
new_start = data["start_date"][0,10]
3
The desired output is:
JavaScript
1
3
1
print new_start
2
2020-01-01
3
Advertisement
Answer
Why are you using a loop? You can do this directly:-
JavaScript
1
2
1
print(date['start_date'][0:10])
2
Output:-
JavaScript
1
2
1
'2020-01-01'
2