Last updated: Apr 8, 2024
Reading timeยท5 min
To split a string into a list of integers:
str.split()
method to split the string into a list of strings.int()
class to convert each string to an integer.my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # ๐๏ธ ['2', '4', '6', '8', '10'] list_of_integers = [int(x) for x in list_of_strings] print(list_of_integers) # ๐๏ธ [2, 4, 6, 8, 10]
On each iteration, we pass the current list item to the int() class and return the result.
You should also use a list comprehension if you need to split the string on each digit.
my_str = '246810' list_of_ints = [int(x) for x in my_str] print(list_of_ints) # ๐๏ธ [2, 4, 6, 8, 1, 0]
We iterate directly over the string and on each iteration, we convert the digit that is wrapped in a string to an integer.
If your string contains non-digit characters, use the str.isdigit()
method to
check if the current character is a digit before passing it to the int()
class.
my_str = 'x y z 2 4 6 8 10 a' list_of_strings = my_str.split(' ') # ๐๏ธ ['x', 'y', 'z', '2', '4', '6', '8', '10', 'a'] print(list_of_strings) list_of_integers = [int(x) for x in list_of_strings if x.isdigit()] print(list_of_integers) # ๐๏ธ [2, 4, 6, 8, 10]
The str.isdigit() method returns
True
if all characters in the string are digits and there is at least 1
character, otherwise False
is returned.
This is a three-step process:
str.split()
method to split the string into a list of strings.map()
function to convert each string into an integer.list()
class to
convert the map object to a list.# ๐๏ธ String containing integers with space-separator my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # ๐๏ธ ['2', '4', '6', '8', '10'] list_of_integers = list(map(int, list_of_strings)) print(list_of_integers) # ๐๏ธ [2, 4, 6, 8, 10]
If your string doesn't contain a separator between the digits, use the following code sample instead.
# ๐๏ธ If you want to split the string on each digit my_str = '246810' list_of_ints = [int(x) for x in my_str] print(list_of_ints) # ๐๏ธ [2, 4, 6, 8, 1, 0]
If the integers in your string are separated with another delimiter, e.g. a
comma, pass a string containing a comma to the str.split()
method.
my_str = '2,4,6,8,10' list_of_strings = my_str.split(',') print(list_of_strings) # ๐๏ธ ['2', '4', '6', '8', '10'] list_of_integers = list(map(int, list_of_strings)) print(list_of_integers) # ๐๏ธ [2, 4, 6, 8, 10]
We used the str.split()
method to split the string into a list of strings.
my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # ๐๏ธ ['2', '4', '6', '8', '10']
The str.split() method splits the string into a list of substrings using a delimiter.
The method takes the following 2 parameters:
Name | Description |
---|---|
separator | Split the string into substrings on each occurrence of the separator |
maxsplit | At most maxsplit splits are done (optional) |
The next step is to convert each string in the list to an integer using the
map()
function.
my_str = '2 4 6 8 10' list_of_strings = my_str.split(' ') print(list_of_strings) # ๐๏ธ ['2', '4', '6', '8', '10'] list_of_integers = list(map(int, list_of_strings)) print(list_of_integers) # ๐๏ธ [2, 4, 6, 8, 10]
The map() function takes a function and an iterable as arguments and calls the function with each item of the iterable.
On each iteration, we pass the string to the int()
class to convert it to an
integer.
map()
function returns a map
object, so we have to use the list()
class to convert it to a list.This is a three-step process:
str.split()
method to split the string into a list of strings.for
loop to iterate over the list.my_str = '2 4 6 8 10' list_of_integers = [] for item in my_str.split(' '): list_of_integers.append(int(item)) print(list_of_integers) # ๐๏ธ [2, 4, 6, 8, 10]
We used a for loop to iterate over the result
of calling str.split()
.
my_str = '2 4 6 8 10' # ๐๏ธ ['2', '4', '6', '8', '10'] print(my_str.split(' '))
On each iteration of the for
loop, we convert the current string to an integer
and append the value to the new list.
The list.append() method adds an item to the end of the list.
You can also use the NumPy module to split a string into a list of integers.
import numpy as np my_str = '2 4 6 8 10' my_list = np.fromstring(my_str, dtype=int, sep=' ').tolist() print(my_list) # ๐๏ธ [2, 4, 6, 8, 10]
You can install NumPy by running the following command.
pip install numpy # ๐๏ธ or with pip3 pip3 install numpy
The numpy.fromstring() method returns a new one-dimensional array initialized from the text in a string.
The method takes a sep argument that is used to determine the split character.
import numpy as np my_str = '2,4,6,8,10' my_list = np.fromstring(my_str, dtype=int, sep=',').tolist() print(my_list) # ๐๏ธ [2, 4, 6, 8, 10]
The
tolist
method converts a numpy
array to a list.
You can also use the re.findall()
method to split a string into a list of
integers.
import re my_str = '2 4 6 8 10 12' list_of_strings = re.findall(r'\d+', my_str) print(list_of_strings) # ๐๏ธ ['2', '4', '6', '8', '10', '12'] list_of_ints = [int(x) for x in list_of_strings if x.isdigit()] print(list_of_ints) # ๐๏ธ [2, 4, 6, 8, 10, 12]
The re.findall() method takes a pattern and a string as arguments and returns a list of strings containing all non-overlapping matches of the pattern in the string.
The first argument we passed to the re.findall()
method is a regular
expression.
The \d
character matches the digits from 0
to 9
(and many other digit
characters).
The plus +
causes the regular expression to match 1 or more repetitions of the
preceding character (the range of numbers).
The re.findall()
method returns a list containing the matches as strings.
The last step is to use a list comprehension to convert the list of strings to a list of numbers.