格式化输出
print(‘你好,明天’)
print(‘你好,北京’)
带变量的输出
name = ‘小婷’
print(name)
加号拼接连接
stu_name = ‘小白’
score = 90
print(stu_name,name)
print(‘学生姓名:’ + stu_name + ‘学生成绩:’ + str(score)) 整数和字符串不能用加号连接
如果,变量较多,加号拼接字符串比较麻烦
name = ‘小白’
score = 90
sex = ‘female’
height = 175
weight = 70
address = ‘郑州市xx街道’
phone = 123454678998
使用格式化输出字符串
方法一:‘姓名%s,成绩%s’ %(变量1,变量2)
% 占位符
%s 字符串,%d 整数,%f 浮点数
print(‘学生姓名:%s,成绩:%s’%(name,score))
方法二:.format() 可以不用转型
print(‘学生姓名:{},成绩:{}’.format(name,score))
不常用写法
print(‘学生姓名:{stu_name},成绩:{stu_score}’.format(stu_name = ‘小明’,stu_score = 100))
小数点精度
print(‘河南省面积:{:.2f}’.format(190.5832))
左对齐右对齐
print(‘{:^20}’.format(‘中心对齐’))
print({:<20}.format(‘左对齐’))
print({:>20}.format(‘右对齐’))
print()输出后不换行
光标不换行
print(‘1’,end=‘’)
print(‘2’)
>>>输出‘12’