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%

相关推荐
工程师007几秒前
C#中的AutoUpdater自动更新类
开发语言·c#·自动更新开源库·autoupdate
lsx2024064 分钟前
Java 泛型
开发语言
喵手11 分钟前
Python爬虫零基础入门【第二章:网页基础·第1节】网页是怎么工作的:URL、请求、响应、状态码?
爬虫·python·python爬虫实战·python爬虫工程化实战·python爬虫零基础入门·网页基础
jghhh0118 分钟前
基于MATLAB的可见光通信系统仿真实现
开发语言·matlab
xiaoqider28 分钟前
C++模板进阶
开发语言·c++
yaonoran29 分钟前
【无标题】
java·开发语言·变量
康小庄34 分钟前
浅谈Java中的volatile关键字
java·开发语言·jvm·spring boot·spring·jetty
移幻漂流40 分钟前
C/C++并发编程详解:如何写出优秀的并发程序
c语言·开发语言·c++
忧郁的橙子.43 分钟前
26期_01_Pyhton判断语句
python
快乐小胡!1 小时前
【自动化测试】Selenium选择/定位元素的基本方法
python·selenium·测试工具