Python: Input & Output

Last modified 4 years ago / Edit on Github
Danger icon
The last modifications of this post were around 4 years ago, some information may be outdated!


# 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))


Using # on each line.

# print("This is not showed.")
print("This is showed.)
This is showed.
# Print normally
print("Hello!") # python 3
print "Hello!" # python 2
# print with `format`
print("Hello {} and {}.".format("A", "B"))
Hello A and B.
# change 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.
# long strings
print('This is a part of sentence.'
'This is other part.')
This is a part of sentence. This is other part.
# print decimals
# print decimals
# print multiples
print("1", 5, "thi") # there are spaces
# print multiples
1 5 thi
# first 20 characters, second 10 characters
print( '{:<20s} {:<10s}'.format(string_one, string_two) )

Display separated results (like in executing multiple code cells),



Check more here.

import logging
log = logging.getLogger(__name__)

# order of increasing severity

# by default, the logging module logs the messages with a severity level of WARNING or above
# thus: debug and info aren't show by default

If the doesn't work, set below[ref],

logging.getLogger().setLevel(logging.INFO) # show all except "debug"
# or
logging.basicConfig(level=logging.DEBUG) # show all
# in the class
import logging
log = logging.getLogger(__name__)"abc")
log.debug("xyz")"abc: %s", abc)
log.debug("xyz: {}".format(xyz))
# in the jupyter notebook
import logging
log = logging.getLogger(__name__)
logging.basicConfig(level=os.environ.get("LOGLEVEL", "DEBUG"))

💬 Comments

Support Thi Support Thi