- 输入函数input()
- 输出函数print( )
输入函数input()
功能:接收用户输入的信息,并以字符型保存 str
语法:input('请输入你的信息')
示例:
name = input('请输入你的姓名:') # 输入:张三
age = input('请输入你的年龄:') # 输入:25
print(f'{name}今年{age}啦!') # 张三今年25岁啦!
注意:
a = input('输入数字a:') # 输入:10
b = input('输入数字b:') # 输入:10
print(f'a+b的和是:{a+b}') #结果:应该是10+10 = 20,但是:1010
代码调整:
a = int(input('输入数字a:')) # int() 转换为整数
b = int(input('输入数字b:')) # int() 转换为整数
print(f'a+b的和是:{a+b}') : # 20
输出函数print( )
功能:向控制台或文件输出结果
语法:默认输出:print('输出信息',)
print('你好,很高兴认识你。')
同行输出:end= ' x ' 【换行输出:默认换行输出】
print('hello','world','Python') # 同行输出
hello world Python
print('hello''world''Python') # 同行输出
helloworldPython
print('hello', end=' ') # 同行输出
print('world', end=' ') # 同行输出
print('Python',end=' ') # 同行输出
hello world Python
连接输出:sep= ' x '
print('hello','world','Python',sep=' * ') # 连接字符
# hello * world * Python 用 * 号连接
输出内容:
数字:
print(520) # 520
print(12.5) # 12.5
字符串:
print('hellowordle') # hellowordle
print("你好") # 你好
表达式:
print(5+2) # 7
print(5*2) # 10
输出位置:默认在控制台上输出(上述输出都在控制台上)
文件:
fp = open('d:/text.txt','a+') # 打开文件对象
print('hello world',file=fp) # 输出内容到文件中
fp.close() # 关闭并写入内容
参考示例:代码转换
print(ord('中')) # 20013
print(chr(20013)) # 中
print(bytes('中','utf-8')) # b'\xe4\xb8\xad'
print(bytes('国','utf-8')) # b'\xe5\x9b\xbd'
print(bytes('生'.encode('unicode-escape'))) # b'\\u751f'
print(bytes('化'.encode('unicode-escape'))) # b'\\u5316'
print(bytes('危'.encode('unicode-escape'))) # b'\\u5371'
print(bytes('机'.encode('unicode-escape'))) # b'\\u673a'