In this post
Input
Get input from user and display (input’s type is string
):
age = input("Your age? ") # python 3, raw_input for python 2
print("Your age:", age) # don't need space after "age"
Get the input and store to numbers list
numbers = list(map(int, input().split()))
Get multi inputs on 1 line:
x, y, z, n = (int(input()) for _ in range(4))
Print strings
Print normally:
print("Hello!") # python 3
print "Hello!" # python 2
Hello!
Hello!
Print with format
:
print("Hello {} and {}.".format("A", "B"))
Hello A and B.
Change the order:
print("Hello {2} and {1}.".format("A", "B"))
Hello B and A.
Directly insert (python 3.6 or above):
b = "B"
print(f'Hello {"A"} and {b}.')
Hello A and B.
Print numbers
Print up to number of decimals:
number = 1
print("{:.6f}".format(number))
1.000000
Print multiple
They are separated by spaces.
print("1", 5, "thi")
1 5 thi