The input mode of python3 and multiple input methods

  • 2020-12-20 03:40:56
  • OfStack

python3 input mode

1. Read keyboard input

The built-in function input() receives standard keyboard input


str = input(" Please enter the ")
print(str)

The default is to return a string type that can be cast to another type


num = int(input(" Please enter the "))
print(num, type(num))
//type( variable ) , returns the type of the variable 

2. raw_input()(python2 only)

The input() and raw_input() functions are basically interchangeable, but input assumes that your input is a valid Python expression and returns the result. This is the biggest difference between the two.


a=input([x+1 for x in range(2,10,2)])
print(a)

[3, 5, 7, 9]

3. sys.stdin.readline()

sys.stdin.readline () treats all input as a string and includes the newline character '\n' at the end, which can be dropped by sys.stdin.readline ().strip ('\ n').


import sys
c = sys.stdin.readline()
print(c,type(c))

Multiple sets of input


a=int(input())
i=0
while i<a:
 b=int(input())
 print(b)
 i=i+1

a=input().split()
for x in a:
 print(int(x))

while True:
 a = sum(map(int, input().split()))
 if(a==0):
  exit(0)
 else:
  print(a)

1 line of multiple value inputs


a,b = map(int,input().split())
print(a,b)

Related articles: