输出
用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()
会依次打印每个字符串,遇到逗号“,”会输出一个空格,因此,输出的字符串是这样拼起来的:
输入
name = input('please enter your name: ')
print('hello,', name)
再次运行这个程序,你会发现,程序一运行,会首先打印出please enter your name:
,这样,用户就可以根据提示,输入名字后,得到hello, xxx
的输出:
C:\Workspace> python hello.py
please enter your name: Michael
hello, Michael
需要注意的是input( )函数获得的用户输入为字符串,若想输入整数,则需将字符串转化为整数
s = input('birth:')
b = int(s) #输入1997变为整数