I use this the following input to enter a list of numbers, for example this list: 1 2 3 4 5 6
a=stdin.readline().strip().split()
if I give print(a)
it the show me ['1','2','3','4','5','6']
, my question is if that input can be modified in such a way that it shows me the list of integers ( [1,2,3,4,5,6]
) but not of strings, or is it simply not possible?
You must explicitly cast a
int
, for example using compressed lists:Or using
input()
:Note that entering a value that cannot be converted to
int
will result in an exception (ValueError: invalid literal for int() with base 10
), this includes substrings with the decimal point ('4.2', '7.25', etc). Whether to accept signed integers ('-3'. '-4', '+7', etc).