Converting data · Python How-To Guides
Converting values from one type to another is a daily task. Each guide gives the shortest working answer, runnable code, the output, what can go wrong and how to handle bad input.
All 10
How to Convert a String to an Int in Python
Use `int(text)`, for example `int("42")` returns `42`. If the string is not a whole number, such as `"3.7"` or `"abc"`, `int()` r…
How to Convert an Int to a String in PythonUse `str(number)`, for example `str(42)` returns `"42"`. For formatting such as padding or commas, use an [f-string](/python-f-st…
How to Convert a List to a String in PythonUse `", ".join(items)` to glue a list of strings into one string. If the list holds numbers, convert them first: `", ".join(map(s…
How to Convert a String to a List in PythonUse `text.split()` to get a list of words, `text.split(",")` to split on a separator, or `list(text)` to get a list of single cha…
How to Convert a String to a Float in PythonUse `float(text)`, for example `float("3.14")` returns `3.14`. Text that is not a number, such as `"abc"` or `"1,234.5"`, raises …
How to Convert a Float to an Int in Python`int(3.9)` returns `3` because it cuts off the decimal part. Use `round(3.9)` to get `4`, or `math.floor()` and `math.ceil()` to …
How to Convert a List to a Dictionary in PythonUse `dict(zip(keys, values))` to build a dictionary from two lists, or `dict(list_of_pairs)` when each item is already a `(key, v…
How to Convert a Tuple to a List in PythonUse `list(my_tuple)`, for example `list((1, 2, 3))` returns `[1, 2, 3]`. The unpacking form `[*my_tuple]` does the same.
How to Convert a String to a Datetime in PythonUse `datetime.strptime(text, format)`, for example `datetime.strptime("2026-03-15", "%Y-%m-%d")`. For ISO 8601 text, `datetime.fr…
How to Convert JSON to a Dictionary in PythonUse `json.loads(text)` to turn a JSON string into a dictionary, or `json.load(file)` to read it from an open file.
Explore more
Python ReferencePython ProgramsPython ComparisonsPython Cheat SheetsPython glossaryPython tutorial