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%

相关推荐
dev派1 分钟前
AI Agent 系统中的常用 Workflow 模式(2) Evaluator-Optimizer模式
python·langchain
前端付豪2 小时前
AI 数学辅导老师项目构想和初始化
前端·后端·python
用户0332126663672 小时前
将 PDF 文档转换为图片【Python 教程】
python
悟空爬虫3 小时前
UV实战教程,我啥要从Anaconda切换到uv来管理包?
python
dev派4 小时前
AI Agent 系统中的常用 Workflow 模式(1)
python·langchain
明月_清风6 小时前
从“能用”到“专业”:构建生产级装饰器与三层逻辑拆解
后端·python
曲幽15 小时前
数据库实战:FastAPI + SQLAlchemy 2.0 + Alembic 从零搭建,踩坑实录
python·fastapi·web·sqlalchemy·db·asyncio·alembic
用户83562907805120 小时前
Python 实现 PowerPoint 形状动画设置
后端·python
ponponon21 小时前
时代的眼泪,nameko 和 eventlet 停止维护后的项目自救,升级和替代之路
python
Flittly21 小时前
【从零手写 ClaudeCode:learn-claude-code 项目实战笔记】(5)Skills (技能加载)
python·agent