Sometimes, we want to split a string of space separated numbers into integers with Python.
In this article, we’ll look at how to split a string of space separated numbers into integers with Python.
How to split a string of space separated numbers into integers with Python?
To split a string of space separated numbers into integers with Python, we can use the map and list functions and string’s split method.
For instance, we write:
n = list(map(int, "42 0".split()))
print(n)
We call split to split the string into a list.
Then we call map with int to map the list entries into integers.
Finally, we call list to convert the map into a list.
Therefore, n is [42, 0].
Conclusion
To split a string of space separated numbers into integers with Python, we can use the map and list functions and string’s split method.