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%

相关推荐
逆境不可逃8 分钟前
写给编程新手的 Prompt 指南
人工智能·python·prompt
Дерек的学习记录2 小时前
C++:stack和queue
开发语言·c++·学习·算法
烟烟Abandon3 小时前
FFmpeg开发基础之 C 语言 :再论指针(讲透指针)
c语言·开发语言·ffmpeg
阿俊-全栈开发6 小时前
扩展设计解析:LikeShop Go 版商城营销活动的插件化扩展设计思路
开发语言·微服务·golang·likeshop·likeshop源码
干啥啥不行,bug第一名7 小时前
EAFNOSUPPORT、EPROTOTYPE、EOPNOTSUPP:socket 的三层参数没有对齐
开发语言·php
期货资管源码7 小时前
内盘期货中远期与跨品种套利开发笔记:价差、配对与执行细节
笔记·python·量化交易·期货·套利
我要见SA姐18 小时前
用生活化类比当钥匙,一键打开 C++ 入门大门
java·开发语言·c++
1024奇点8 小时前
第四篇 第 03 课
开发语言·ide·后端
AI大模型-小雄8 小时前
Codex 如何自动修复项目 Bug?从错误定位到测试验证的实战教程
python·程序员·bug·ai编程·codex·chatgpt plus·chatgpt pro
大彼方..8 小时前
CSDN-02-开发环境介绍与安装
c语言·开发语言·stm32