Converting a string to integers and assigning them to variables

Viewed 155

Example I have line that is "3 2 11" . I want to split that into 3 so "3" , "2", "11" and the convert them into integers and assign them into individual variables my code so far below:

import sys
for line in sys.stdin:
    print(line, end="")

snailHasNotEscaped = True
days = 0
totalMs = 0

mPerWholeDay = N - M

while snailHasNotEscaped:
   totalMs += mPerWholeDay
   days += 1
   if totalMs >= H:
      snailHasNotEscaped = False
6 Answers

You can use the map function:

x, y, z = map(int, input().split())

Or if you want to use Fast I/O

from sys import stdin
x, y, z = map(int, stdin.readline().split())

Try something like this:

numericString = "3 2 111".split() # Define str and split
listOfNumbers = list() # Create a new list
for s in numericString: # Loop the splitted elements
    listOfNumbers.append(int(s)) # Convert string to integer and append
print(listOfNumbers) # Print the list

Its a oneliner

output = map(int, input.split())

if no of variable is fixed in output then

x, y, z = output 

or

 x, y, z =  map(int, input.split())

I will assume that you have unknown amount of variables so ill store them on a list.
I will use list comprehension to build the new list:

line = "3 2 11"
lst = line.split()
int_lst = [int(ele) for ele in lst]
print(int_lst)

However you can use for instead:

line = "3 2 11"
lst = line.split()
int_lst = []
for ele in lst:
    int_lst.append(int(ele))
print(int_lst)

@sean. How it going ? Let me help u :)

Imagine your string is

my_string = "3 2 11"

First you need to split it for each number, like this:

my_string.split() 

or

my_string.split(' ')  #choose your delimiter (ex: '.', ';', ',')

and than Python will return a list like this

>> [3,2,11]

Now you just need to assign to the variable using the function int():

a = int(my_string.split()[0]) #For the fist index on the list "3"
b = int(my_string.split()[1]) #For the second index on the list "2" ...
c = int(my_string.split()[2]) #For the second index on the list "11"

or

a = int(my_string.split(' ')[0]) #For the fist index on the list "3"
b = int(my_string.split(' ')[1]) #For the second index on the list "2" ...
c = int(my_string.split(' ')[2]) #For the second index on the list "11"

Hello,

reading your description - here is something I would try: Note: If I have misunderstood something, I would be happy if you/someone pointed it out!

>>> s1="3 2 11"     # the string "3 2 11"
>>> s2=s1.split()   # same string, but split.

>>> s1
'3 2 11'

>>> s2
['3', '2', '11']

>>> ([int(i) for i in s2])  # use type() with a for loop 
[3, 2, 11]                  # to print out every element in the split str `s2`,
                            # as the type int.

# we can also,
# access one element in s2,
# and check what type that element is:
# by (for example) assigning it to a variable "element_1"

>>> element_1 = ([int(i) for i in s2])[0]
>>> element_1
3

# Check what type it is:
>>> type(element_1)
<class 'int'>

## A example of using + and *:

>>> a=([int(i) for i in s2])[0]
>>> b=([int(i) for i in s2])[1]
>>> c=([int(i) for i in s2])[2]

>>> a,b,c
(3, 2, 11)

>>> (a*b)+c
17

Related