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?

3
  • Are you familiar with the built-in input() function? Commented Sep 17, 2020 at 13:04
  • Please update the question to show the code you’ve written so far to solve the issue. Commented Sep 17, 2020 at 13:05
  • @Tomerikoo Yes it answers my question. thanks a lot Commented Sep 17, 2020 at 13:12

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

3 Comments

why not use input()?
@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.
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...
0

Try this:

ip = input().split()

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

print(text, num1, num2, num3)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.