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及以上 ...
随机推荐
- Golang标准库之bytes介绍
本次主要介绍golang中的标准库bytes,基本上参考了 字节 | bytes .Golang标准库--bytes 文章. bytes库主要包含 5 大部分,即: 常量 变量 函数 Buffer R ...
- MindSponge分子动力学模拟——使用MDAnalysis工具进行后分析(2024.02)
技术背景 分子动力学模拟(Molecule Dynamics Simulation,MD),本质上是一门采样技术.通过配置力场参数.拓扑结构和积分器,对一个给定的体系不断的采样,最终得到一系列的轨迹. ...
- 【Azure Redis 缓存】Azure Redis 4.0 被扫描到漏洞,如何修补呢?
问题描述 在安全级别要求高的公司中,任何系统都会进行安全扫描.比如Azure 云上的Redis服务,也在扫描的范围中,最后发现Redis 4.0存在以下漏洞: CVE-2019-10192:https ...
- C#多线程(5):资源池限制
目录 Semaphore.SemaphoreSlim 类 Semaphore 类 示例 示例说明 信号量 SemaphoreSlim类 示例 区别 Semaphore.SemaphoreSlim 类 ...
- Jmeter Jsonpath 语法你了解多少?
- Metasploitable3 渗透测试
1.信息手机阶段 信息收集经常使用的软件 功能也比较强大的Nmap Nmap nmap -p- -sS -sV -n -v --reason --open -oX demon.xml 192.168. ...
- 重新定义 vscode 命令行工具 code命令 code $profile
vscode 默认命令行有问题 他那个每次都打开cli.js 目录名里面有空格 要 &开头后面跟双引号 所以从新定义后 变量是 $变量名 前面再加上& 就能调用那个exe了 后面再跟上 ...
- SourceTree 摘樱桃 === 遴选 [不要使用这个功能!!不要使用!不要使用!]
SourceTree 摘樱桃 === 遴选 不要使用摘樱桃!!不要使用!不要使用! 我找了一个文本的git,进行的测试,发现很不好用,文档我又恢复过来了,因为就改了几个字,代码的话,会造成 不可挽回的 ...
- gcc编译stm32 f103出现错误init.c:(.text.__libc_init_array+0x20): undefined reference to `_init'
解决方法: 方法一:去掉makefile中的编译选项:-nostartfiles 方法二:方法一不凑效的情况下,添加编译选型:--specs=nano.specs
- 一个简易的ORM框架的实现(二)
框架目标 什么是框架,框架能做到什么? 把一个方向的技术研发做封装,具备通用性,让使用框架的开发者用起来很轻松. 属性: 通用性 健壮性 稳定性 扩展性 高性能 组件化 跨平台 从零开始-搭建框架 建 ...