Learning Python 002 print() 和 input()

时间:2023-03-08 21:14:44

Python print() 和 input()

print()函数

print()函数可以向终端中输入指定的内容。

输出当个字符串

.py文件中,输入下面的代码,并保存:

print('hello world')
> demo.py
hello world

终端中执行:

>>> print('hello world')
hello world

输出多个字符串

.py文件中,输入下面的代码:

print('Aobo', 'Sir', 'Learning', 'Python')
> demo.py
('Aobo', 'Sir', 'Learning', 'Python')

终端中执行:

>>> print('Aobo', 'Sir', 'Learning', 'Python')
Aobo Sir Learning Python

Python交互模式print()会依次打印每个字符串,遇到逗号“,”会输出一个空格

Python.py文件模式,print()会打印出除print这个函数名之外的所有的信息。

.py文件中,输入下面的代码:

print('32 + 32 =', 32+32)
> demo.py
('32 + 32 =', 64)

终端中执行:

>>> print('32 + 32 =', 32+32)
32 + 32 = 64

input()函数

.py文件中输入下面的代码:

name = input('What\'s your name?')
print('Hello, ' + name + '!')
>demo.py
What's your name? :

输入的内容,需要用引号括上。

What's your name? : 'AoboSir'
Hello, AoboSir!

终端中执行:

>>> name = input('What\'s your name?')
What's your name? :

输入你的名字:(不需要被引号括上)

What's your name? : AoboSir
Hello, AoboSir!

参考网站:

http://www.liaoxuefeng.com/wiki/0014316089557264a6b348958f449949df42a6d3a2e542c000/001431643484137e38b44e5925440ec8b1e4c70f800b4e2000