Skip to content

Python map string to int | Example code

  • by

Using the map() function you can convert a list of strings into a list of int (number). Use the map() function in Python to apply a function (in this case, int()) to every element in a list, and get back a new list with the modified values.

Python map string to int example

A simple example code converts the list to an integer list using the map function.

T1 = ['13', '17', '18', '21', '32']

res = list(map(int, T1))

print(res)

Output:

Python map string to int

Another example

# Define a list of strings
num_strings = ['10', '20', '30', '40', '50']

# Use map() to convert the strings to integers
num_ints = list(map(int, num_strings))

# Print the resulting list of integers
print(num_ints)

In this example, we define a list of strings called num_strings. We then use the map() function to apply the int() function to each element in the list. The resulting map object is then converted to a list using the list() function, and assigned to the variable num_ints.

Comment if you have any doubts or suggestions on this Python map string program.

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading