Python格式化字符串:%、format、f-string
目前Python格式化字符串的方式有三种:
1. %
2.format
3.f-string
% 格式化常用方法:
# % 格式化字符串
s1 = 'name is %s' % ('zhangsan')
# >>> name is zhangsan # % 格式化整数
s2 = 'age is %d' % (12)
# >>> age is 12 # % 格式化整数,指定位数,用0填充
s3 = 'today is %02d' % (8)
# >>> today is 08 # % 格式化浮点数,默认保留6位小数
s4 = 'PI = %f' % (3.1415)
# >>> PI = 3.141500 # % 格式化浮点数,保留2位小数
s5 = 'PI = %.2f' % (3.1415)
# >>> PI = 3.14 # % 格式化浮点数,不带小数
s6 = 'PI = %.0f' % (3.1415)
# >>> PI = 3
format 格式化常用方法:
# format 格式化字符串
s1 = 'name is {}'.format('zhangsan')
# >>> name is zhangsan # format 格式化整数
s2 = 'age is {}'.format(12)
# >>> age is 12 # format 格式化整数,指定位数,用0填充
s3 = 'today is {:0>3d}'.format(8)
# >>> today is 008 # format 格式化整数,以逗号分隔
s4 = 'number is {:,}'.format(123456789)
# >>> number is 123,456,789 # format 格式化整数,指数记法
s5 = 'number is {:.2e}'.format(123456789)
# >>> number is 1.23e+08 # format 格式化浮点数
s6 = 'PI = {}'.format(3.1415)
# >>> PI = 3.1415 # format 格式化浮点数,保留2位小数
s7 = 'PI = {:.2f}'.format(3.1415)
# >>> PI = 3.14 # format 格式化浮点数,带符号保留两位小数
s8 = 'PI = {:+.2f}'.format(-3.1415)
# >>> PI = -3.14 # format 格式化浮点数,百分比显示
s9 = 'number is {:.2%}'.format(3.1415)
# >>> number is 314.15% # format 格式化浮点数,不带小数
s10 = 'PI = {:.0f}'.format(3.1415)
# >>> PI = 3
f-string 格式化常用方法:
data1 = 'zhangsan'
data2 = 123456789
data3 = 3.1415 # f 格式化字符串
s1 = f'name is {data1}'
# >>> name is zhangsan # f 格式化整数
s2 = f'number is {data2}'
# >>> number is 12 # f 格式化整数,指定位数,用0填充
s3 = f'number is {data2:010d}'
# >>> number is 0123456789 # f 格式化浮点数
s4 = f'PI = {data3}'
# >>> PI = 3.1415 # f 格式化浮点数,保留2位小数
s5 = f'PI = {data3:.2f}'
# >>> PI = 3.14 # f 格式化浮点数,不带小数
s6 = f'PI = {data3:.0f}'
# >>> PI = 3
Python格式化字符串:%、format、f-string的更多相关文章
- Python格式化字符串--format
format格式化字符串方法相较于老版%格式方法的优点: 1.不需要理会数据类型的问题,在%方法中'%s'只能替代字符串类型. 2.单个参数可以多次输出,参数顺序可以不相同. 3.填充方式十分灵活,对 ...
- python格式化字符串format函数
1. format可以接受无限个的参数,位置可以不按顺序: In [1]: "{} {}".format("hello","world") ...
- python格式化字符串format的用法
填充与对齐 填充常跟对齐一起使用 ^.<.>分别是居中.左对齐.右对齐,后面带宽度 :号后面带填充的字符,只能是一个字符,不指定的话默认是用空格填充 比如 In [15]: '{:> ...
- Python 的格式化字符串format函数
阅读mattkang在csdn中的博客<飘逸的python - 增强的格式化字符串format函数>所做笔记 自从python2.6开始,新增了一种格式化字符串的函数str.format( ...
- Python格式化字符串~转
Python格式化字符串 在编写程序的过程中,经常需要进行格式化输出,每次用每次查.干脆就在这里整理一下,以便索引. 格式化操作符(%) "%"是Python风格的字符串格式化操作 ...
- Python格式化字符串知多少
字符串格式化相当于字符串模板.也就是说,如果一个字符串有一部分是固定的,而另一部分是动态变化的,那么就可以将固定的部分做成模板,然后那些动态变化的部分使用字符串格式化操作符(%) 替换.如一句问候语: ...
- [编程基础] Python格式化字符串常量f-string总结
Python格式化字符串常量f-string总结 本文主要总结在Python中如何使用格式化字符串常量f-string(Formatted string literals).在 Python 程序中, ...
- Python格式化字符串和转义字符
地址:http://blog.chinaunix.net/uid-20794157-id-3038417.html Python格式化字符串的替代符以及含义 符 号 说 明 ...
- Python格式化字符串(f,F,format,%)
# 格式化字符串: 在字符串前加上 f 或者 F 使用 {变量名} 的形式来使用变量名的值 year = 2020 event = 'Referendum' value = f'Results of ...
- 【转】Python格式化字符串str.format()
原文地址:http://blog.xiayf.cn/2013/01/26/python-string-format/ 每次使用Python的格式字符串(string formatter),2.7及以上 ...
随机推荐
- 【Azure Function App】如何修改Azure函数应用的默认页面呢?
问题描述 当在Azure中创建了一个函数应用(Function App)后,访问默认URL会得到一个默认的页面.是否有办法修改这个默认页面呢? 问题解答 在之前的博文中,介绍了修改App Servic ...
- 【Azure 应用服务】如何关掉App Service/Function App的FTP部署, 使之变成FTPS
问题描述 如何关掉App Service/Function App的FTP部署, 使之变成FTPS方式呢? 问题解答 在应用服务/函数应用的配置下选择右边的常规设置,然后修改FTP状态为"仅 ...
- 【Azure Redis 缓存】Redis的指标显示CPU为70%,而Service Load却达到了100%。这两个指标意义的解释及如何缓解呢?
问题描述 为什么Redis的指标显示CPU为70%,而Service Load却达到了100%, 如何来解释这两个指标,以及如何来缓解这样的情况呢? 问题回答 CPU指标:该值表示的是用于 Redis ...
- UserWarning: To get the last learning rate computed by the scheduler, please use `get_last_lr()`
将 lr = scheduler.get_lr() 改为 lr = scheduler.get_last_lr()
- [Linux] Linux 自动挂载mount --bind 实现类似目录硬链的效果 (包含ZFS方案)
说明 这个命令用以将一个目录挂载到另一个目录,以实现类似于硬链的操作 但是这个命令只是在内存中建立了一个映射,重启系统之后挂载就消失了 而linux是不支持目录硬链的,具体原因见linux为什么不能硬 ...
- 通过 TCPView KPKIService.exe 删掉 (原来是单点登录的中间件)
叫 统一安全中间件,就是个第三方做的key的安全检查,谁知道是哪年装的 (原来是单点登录的中间件) 资料 https://baijiahao.baidu.com/s?id=17173842191483 ...
- 尚硅谷Java 宋红康2023版 - 学习笔记
尚硅谷Java 宋红康2023版 - 学习笔记 观看地址 https://www.bilibili.com/video/BV1PY411e7J6 60-IDEA开发工具-HelloWorld的编写与相 ...
- 基于ADS1292芯片的解决方案之源码解析
接口解析 A 该芯片和主控使用的是SPI接口通信的. SPI接口一般有四根线,确保四根线准确连接是对的. B 该芯片可以有中断模式数据触发,所以,主控mcu需要有外部中断处理流程. //DRDY中断 ...
- 23_FFmpeg像素格式转换
简介 前面使用 SDL 显示了一张YUV图片以及YUV视频.接下来使用Qt中的QImage来实现一个简单的 YUV 播放器,查看QImage支持的像素格式,你会发现QImage仅支持显示RGB像素格式 ...
- 关于C++ 多态实现技术的深度解析(vfptr,vftable)
PS:要转载请注明出处,本人版权所有. PS: 这个只是基于<我自己>的理解, 如果和你的原则及想法相冲突,请谅解,勿喷. 前置说明 本文作为本人csdn blog的主站的备份.(Bl ...