Python的输入与输出
导读:Python的输入与输出Python输入Python输出print( 输出format( 格式化输出Python输入在Python中,使用内置函数input( 可以接收用户的键盘输入 input( 函数的基本用法如下:variable...
Python的输入与输出
- Python输入
- Python输出
- print()输出
- format()格式化输出
Python输入
在Python中,使用内置函数input()可以接收用户的键盘输入
input()函数的基本用法如下:variable = input()
参数说明如下:
variable
:为保存输入结果的变量
双括号内的文字用于提示要输入的内容
实例
:接收用户输入的结果并保存到demo变量中
demo = input("请输入内容:")
✅在Python3.x中,input()接收用户键盘输入的类型默认为字符串类型,如果想用input()函数接收其他数据类型的数据(例如int类型)就需要进行强转数据类型
number = int(input("请输入整数:"))
Python输出
print()输出
在Python中,可以使用print()函数将结果输出到控制台
print()函数语法格式:print(输出内容)
其中,输出内容可以是数字,字符串,也可以是包含运算符的表达式
a = 7
b = 10
print(7) # 输出数字
print(a if a >
b else b) # 输出条件表达式的结果
print("人生苦短我用Python") # 输出字符串
format()格式化输出
✅简单了解后面讲到字符串的时候会详细进行讲解
- 使用
.format()
格式化输出
name = "hacker"
age = 20
print("My name is {
}
and I am {
}
years old.".format(name, age))
- 除了使用
.format()
以外,还可以使用f.string
来进行更简洁,易读的输出
name = "hacker"
age = 20
print(f"My name is {
name}
and I am {
age}
years old.")
以上就是Python基础入门篇之Python的输入输出
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Python的输入与输出
本文地址: https://pptw.com/jishu/6672.html