---
# System prepended metadata

title: Python Converting from str_dict to dict
tags: [Classical Computing]

---

###### tags: `Classical Computing`

# Python Converting from str_dict to dict
We cannot convert directry from `str` to `dict` by using `dict()`.

## json.loads(str_dict)
Instead we use `json.loads` to change `str_dict` into `dict`. 

```python=
import json

Capitals = '{"Japan": "Tokyo", "USA": "Washington D.C.", "UK": "London"}'

Capitals_dict = json.loads(Capitals)

print(Capitals_dict)
print(type(Capitals_dict))

# Output
# {'Japan': 'Tokyo', 'USA': 'Washington D.C.', 'UK': 'London'}
# <class 'dict'>
```

> Attention
> If the properties (in this case Japan etc)`str_dict` is closed with the single quotations, not the double quotations, error occurs. In this case we use `ast.literal_eval(str_dict)` explained in the next.

##  ast.literal_eval(str_dict)
```python=
import ast

Capitals = "{'Japan': 'Tokyo', 'USA': 'Washington D.C.'}"
Capitals_dict = ast.literal_eval(Capitals)

print(Capitals_dict)

# output
# {'Japan': 'Tokyo', 'USA': 'Washington D.C.'}
```
