一、输出
print(values,sep,end,file)
values:需要输出的值,用‘,’隔开。
sep:分隔符,默认为空格
end:以指定的字符结束,默认为‘\n’。
file:默认是标准输出。
注意:输出变量或表达式时,不加引号;输出字符串时,可用单引号或者双引号引起来;多个数据用‘,’分割。
1>values
>>>c=1100
>>>d=2.85
>>>print(‘The two number are’,a,b)
//The two numbers are 10 58.34
2>sep
>>>print(‘2020’,’11’,’29’,sep=‘-’)
//2020-11-29
3>end
print(“python”)
print(“python”)
//python
python
print(“python”,end=‘ ’)
print(“python”,end=‘ ’)
//python python
二、格式化输出
1>“%[标志±][0][输出最小宽度m].[精度n]格式字符”%变量
([]为可选的,看情况而定)
(±)指定输出对齐方式,‘+’右对齐,默认为右对齐,用‘-’修改为左对齐
>>>stre=‘cyy’
>>>print(“输出:%s”%stre)
//输出:cyy
2>使用format
填充槽{}
顺序(填充,对齐,宽度,‘.’,.精度,格式符号)
a=‘Cyy’
b=18
print(“姓名:{},年龄:{}”.format(name,age))
//姓名:cyy,年龄:18
x=10
y=109.32
s=‘I love the girl in Huizhou’
print(‘{0:2d}{1:3d}{2:4d}{3:.2f}’.format(x,x*x,x*x*x*x,y))
print(‘0:*^30’.format(s))//居中且用‘*’填充
3>f-string
f-string用{}表示被替换字段,{}中可以直接填充替换的内容
(自定义格式:对齐、宽度、符号、补零、精度、进制等)
a,b,c=‘haha’,21,1.85
print(f’笑起来:{a},年龄:{b},身高{c}’)
二、输入
1、input输入
input()输入的是字符串
c=input(“输入密码:”)
print(‘您输入的密码是:’,c)
//输出字符串
c=int(input(‘输入密码:’))
print(‘您输入的密码是:’,c)
//输出整数
2、同时输入多个数据. eval();split();map()和split().
c,d=eval(input(‘请输入两个整数:’))
print(‘两数字之差’,a-b)
eval()函数要求输入的数据以‘,’隔开
a,b=input(’输入两个整数:’).split(‘,’)
a=int(a)
b=int(b)
print(‘两数之积:’,a*b)
split():通过指定分隔符对字符串进行切片,并返回分割后的字符串列表(list)
```python
sc=input(‘请输入两个整数:’)
a,b=map(int,sc.split(‘,’))
print(‘两数之和:’,a+b)
map(function,iterable)
map函数通过将指定的function函数依次作用在给定序列iterable中的每一个元素,得到一个新的list并返回。
print(‘我想看看自己能坚持多久’)
print(‘致G姑娘’)