Python input

How could I take input like
A=30 B=10

1 Like

Your query is not so clear that how output is given.
1). if 30 and 10 have a space between them then
A,B = [int(x) for x in input().split()] or equal to map(int,input().split())
input().split(’ ‘) splita strings with ’ ’ and int(x) for x or map(int, )convert those strings to integers.
extra- input().split(’,') can be used to take 2,3 input.
2). if 30 and 10 are in different lines then
A = int(input())
B = int(input())
go to additional link- Taking multiple inputs from user in Python - GeeksforGeeks

1 Like

Fastest answer (in terms of runtime):

import sys
a,b = list(map(int, sys.stdin.readline().split()))
3 Likes

Naive method:

  • Read an Entire line as String:
    line = input()
  • Read a line containing a Single Integer
    num = int(input())
  • Read a pair of Space separated Integers
    num1, num2 = map(int, input().split())
  • Read a triple of Space separated Integers
    num1, num2, num3 = map(int, input().split())
  • Read a line containing a single, real number
    f = float(input())
  • Read a pair of space separated Real Numbers
    f1, f2 = map(float, input().split())
  • Read a pair of space separated Strings
    s1, s2 = map(str, input().split())
  • Read a list of Integers
    arr = list(map(int, input().split()))
  • (Trickier) Read a line containing a string and an Integer
    s, num = map(str, input().split()) # Consider both of them as Strings
    num = int(num) # Convert num to int

Efficient Method (Recommended for Fast IO):

  • Declaration part
    from sys import stdin, stdout
    read = stdin.readline # read holds the reference to function
  • Read an Entire line as String:
    line = read()
  • Read a line containing a Single Integer
    num = int(read())
  • Read a pair of Space Separated Integers
    num1, num2 = map(int, read().split())
  • Read a list of Space separated Integers
    arr = list(map(int, read().split()))

And so on. If you still need help, feel free to ask.

8 Likes

Using a list and then unpacking it into two or more integers would add a very little overhead.

The following is little better than the above one.

a, b = map(int, stdin.readline().split())

3 Likes

Nice :heart:

1 Like

Thanks

1 Like

a,b=input().split()
_,a=a.split("=")
_,b=b.split("=")

After doing this convert both the value in integer using int()

I will try to cover various input types.

  1. Single input as string-

    S

    s = input()

  2. Multiple strings on different line-

    S_{1}
    S_{2}

    s1 = input()
    s2 = input()

  3. Multiple space separated strings on same line-

    S_{1} S_{2}

    s1, s2 = tuple(input().split())

  4. Single input as integer-

    N

    n = int(input())

  5. Multiple integers on different line-

    N_{1}
    N_{2}

    n1 = int(input())
    n2 = int(input())

  6. Multiple space separated integers on same line-

    N_{1} N_{2}

    n1,n2 = map(int,input().split())

  7. Array of space separated integers -

    A_{1}\ A_{2}\ A_{3}\ A_{4}\ A_{5}\ \cdots \ A_{N}

    A = list(map(int,input().split()))

  8. Matrix of integers as input-

    A_{11}\ A_{12}\ A_{13} \cdots A_{1n}
    A_{21}\ A_{22}\ A_{23} \cdots A_{2n}
    \vdots\ \ \ \ \ \ \ \vdots \ \ \ \ \ \ \vdots \ \ \ \ \ \ \vdots \ \ \ \ \ \ \vdots
    A_{m1}\ A_{m2}\ A_{m3} \cdots A_{mn}

    grid = [list(map(int,input().split())) for i in range m]

Something about the split method-

It is the method available for string (and so it is applied on strings only) which returns a list of string which are separated on the basis of delimiter(the default argument is whitespace).

Map function -

We are using map function here because once we have created a list of strings using the split method now we want to change their type. Map takes arguments as function and an iterable (here int is a function and the list of strings is the iterable object).

2 Likes

import sys
A,B = list(map(int, sys.stdin.readline().split()))
print(A, B)

alternate solution for different lines of input:
A=int(input())
B=int(input())
print(A, B)

this might help you :slight_smile: :v: