Python输入和输出

1. 输出 - print()

print()在括号中加上字符串,就可以向屏幕上输出指定的文字。比如输出'hello, world',用代码实现如下:

>>> print('hello, world')

print()函数也可以接受多个字符串,用逗号“,”隔开,就可以连成一串输出,遇到逗号就输出一个空格:

>>> print('The quick brown fox', 'jumps over', 'the lazy dog')
The quick brown fox jumps over the lazy dog

print()也可以打印整数,或者计算结果:

>>> print(300)
300
>>> print('100 + 200 =', 100 + 200)
100 + 200 = 300

2. 输入 - input()

Python提供了一个input(),可以让用户输入字符串,并存放到一个变量里,括号里面。比如:

>>> name = input('please enter your name: ')
Michael

注意:input()返回的数据类型是strstr不能直接和整数比较,必须先把str转换成整数。Python提供了int()函数来完成这件事情:

s = input('birth: ')
birth = int(s)
if birth < 2000:
    print('00前')
else:
    print('00后')
原文地址:https://www.cnblogs.com/AmyHu/p/10413522.html