python中的print函数总结

文章目录

打印变量

python 复制代码
a=100
print(a)

输出

100

打印数学计算

python 复制代码
a=100
b=50
print(a+b)
print(a*b)

输出

150

5000

多行文本

三个单引号或者又引号定义多行字符串

python 复制代码
str='''第1行
第2行
第3行 '''
print(str)

输出

第1行

第2行

第3行

复制n次字符串 xn,nx

python 复制代码
print('R'*40)
print(40*'R')

输出

RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

RRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRRR

不换行输出多个数据

python 复制代码
a=100
b=50
print(a,b,'两个变量的值')

输出

100 50 两个变量的值

换行符

\n表示 换行符

\t表示水平制表位,用于横向跳到下一个制表位

" 双引号

' 单引号

\ 一个反斜杠

python 复制代码
print('北京\n欢迎您')
# 连续换多行
print('北\n京\n欢\n迎\n您')

输出

北京

欢迎您

制表位

一个制表位8个字符,一个没有填满剩几个空几个字符

python 复制代码
print('hellooooooo')
print('hel\tlooooooo')
print('hell\tooooooo')
print('hello\toooooo')
print('helloo\tooooo')
print('hellooo\toooo')
print('helloooo\tooo')
print('hellooooo\too')
print('helloooooo\to')

输出

hellooooooo

hel looooooo

hell ooooooo

hello oooooo

helloo ooooo

hellooo oooo

helloooo ooo

hellooooo oo

helloooooo o

转义

python 复制代码
print('老板说:\'明天我们就要上市了,大家加个班\'')
print('老板说:\"明天我们就要上市了,大家加个班\"')

输出

老板说:'明天我们就要上市了,大家加个班'

老板说:"明天我们就要上市了,大家加个班"

原字符

在字符串前面加上小写或者大写的r使转义字符失效

python 复制代码
print(r'北京\n欢迎您')
print(R'北京\n欢迎您')

字符串切片

python 复制代码
s='HELLOWORLD'
print(s[0],s[-10]) # 序号0与-10表示的是同一个字符
print('北京欢迎您'[4])  # 您
print('北京欢迎您'[-1])  # 您
print(s[2:7]) # 从2开始7结束,但不包含7
print(s[-8:-3]) # 反向 从-8到-3,但不包含-3
print(s[:5]) # 从0开始到5结束,但是不包含5
print(s[5:]) # 从5开始到结尾

输出

H H

LLOWO

LLOWO

HELLO

WORLD

格式化字符串

python 复制代码
name='张三'
age=18
score=80.5
print('姓名:%s,年龄:%d,成绩:%f'%(name,age,score))
print('姓名:%s,年龄:%d,成绩:%.1f'%(name,age,score)) # 保留一位小数
print(f'姓名:{name},年龄:{age},成绩:{score}')
print('姓名:{0},年龄:{1},成绩:{2}'.format(name,age,score))
print('姓名:{0},年龄:{2},成绩:{1}'.format(name,score,age))

姓名:张三,年龄:18,成绩:80.500000

姓名:张三,年龄:18,成绩:80.5

姓名:张三,年龄:18,成绩:80.5

姓名:张三,年龄:18,成绩:80.5

姓名:张三,年龄:18,成绩:80.5

python 复制代码
s='helloworld'
print('{0:*<20}'.format(s)) # 字符串显示宽度为20,左对齐,空白部分使用*号填充
print('{0:*>20}'.format(s)) # 同上,右对齐
print('{0:*^20}'.format(s)) # 同上,居中对齐
print(s.center(20,'*')) # 同上

helloworld**********

**********helloworld
helloworld
helloworld

千位分隔符(只适用于整数和浮点数)

python 复制代码
print('{0:,}'.format(987654312))
print('{0:,}'.format(987654312.1234))

987,654,312

987,654,312.1234

浮点数小数部分的精度

python 复制代码
print('{0:.2f}'.format(3.1415926))

3.14

字符串类型,.表示最大的显示长度

python 复制代码
`print('{0:.5}'.format('helloworld')) # 最大的显示宽度是5

hello

整数类型

python 复制代码
a=318
print('二进制:{0:b},十进制:{0:d},八进制:{0:o},十六进制:{0:x}'.format(a))

二进制:100111110,十进制:318,八进制:476,十六进制:13e

浮点数类型

python 复制代码
b=3.1415926
print('{0:.2f},{0:.2E},{0:.2e},{0:.2%}'.format(b))

3.14,3.14E+00,3.14e+00,314.16%

相关推荐
数据智能老司机5 小时前
精通 Python 设计模式——分布式系统模式
python·设计模式·架构
数据智能老司机6 小时前
精通 Python 设计模式——并发与异步模式
python·设计模式·编程语言
数据智能老司机6 小时前
精通 Python 设计模式——测试模式
python·设计模式·架构
数据智能老司机6 小时前
精通 Python 设计模式——性能模式
python·设计模式·架构
c8i6 小时前
drf初步梳理
python·django
每日AI新事件6 小时前
python的异步函数
python
这里有鱼汤7 小时前
miniQMT下载历史行情数据太慢怎么办?一招提速10倍!
前端·python
databook16 小时前
Manim实现脉冲闪烁特效
后端·python·动效
程序设计实验室17 小时前
2025年了,在 Django 之外,Python Web 框架还能怎么选?
python
倔强青铜三18 小时前
苦练Python第46天:文件写入与上下文管理器
人工智能·python·面试