0

I have an input where the first line is a string and the next line contains three integers:

StackOverflow

1 2 3

I have to store the string from the first line to a separate variable and the 3 integers in separate variables. The input is comming fromstdin.

I tried to use sys.stdin.read() but it reads the entire input as one entity. How to split it to separate variables?

Rostislav Matl
  • 4,294
  • 4
  • 29
  • 53
venkat
  • 13
  • 6

2 Answers2

1

use readline() it helps in reading input per line.

import sys
string_inp=sys.stdin.readline()
a,b,c=map(int,sys.stdin.readline().split())
print(string_inp)
print(a,b,c)

This way, you'll get integers in 3 different variables and string in another.

use input as:

StackOverflow
1 2 3
Yash
  • 1,271
  • 1
  • 7
  • 9
  • why not use `input()`? – Tomerikoo Sep 17 '20 at 13:19
  • @Tomerikoo you can definately use ```input()``` but since OP tried using ```sys.stdin.read()```, I am only trying to point out how a small change could have fixed his problem. – Yash Sep 17 '20 at 13:21
  • Oh true, I didn't notice OP said he is using that. In that case that's a good correction but I would still mention about using `input()` as this is the idiomatic way of getting user input... – Tomerikoo Sep 17 '20 at 13:56
0

Try this:

ip = input().split()

text = ip[0]
num1, num2, num3 = (int(x) for x in ip[1:])

print(text, num1, num2, num3)
Md Zafar Hassan
  • 284
  • 3
  • 13