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及以上 ...
随机推荐
- 逆向实战32——某东最新h5st4.4算法分析
前言 本文章中所有内容仅供学习交流,抓包内容.敏感网址.数据接口均已做脱敏处理,严禁用于商业用途和非法用途,否则由此产生的一切后果均与作者无关,若有侵权,请联系我立即删除! 目标网站 aHR0cHM6 ...
- 浅入 ABP系列(3):增加日志组件、依赖注入服务
目录 自动依赖注入 添加日志依赖 添加日志功能 依赖注入 版权护体作者:痴者工良,微信公众号转载文章需要 <NCC开源社区>同意. 前面两篇我们搭建了一个基础的.简单的,具有统一响应格式的 ...
- Netty笔记(2) - 基本实现与异步模型
示例内容: 服务端监听6668端口 , 客户端连接 并发送信息给服务端 ,服务端收到信息打印 并返回信息给客户端 服务端代码: public class NettyServer { public st ...
- netcore 图片缩略图
/// <summary> /// 取小写文件名后缀 /// </summary> /// <param name="name">文件名< ...
- Swagger (API框架,API 文档 与API 定义同步更新)
1.依赖 <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring ...
- 【刷题】LeetCode 239 滑动窗口最大值- Python手撕最大堆
手撕版 最大堆的完全实现, 堆中元素为二元组(num, idx),比较时用数值,赋值或交换时用整个元组. class Heap: def __init__(self, arr, capacity): ...
- Android TextView自动缩放能够完整显示出一行
原文地址: Android TextView自动缩放能够完整显示出一行 - Stars-One的杂货小窝 app开发中,需要TextView可以在不同的屏幕大小要完整显示出文字,而不是显示省略号 可以 ...
- Java递归实现全排列改进(二)---利用ArrayList实现去重
import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Test{ priv ...
- 什么会导致JAVA应用程序的CPU使用率飙升
问题 无限循环的while会导致CPU使用率飙升吗? 经常使用Young GC会导致CPU占用率飙升吗? 具有大量线程的应用程序的CPU使用率是否较高? CPU使用率高的应用程序的线程数是多少? 处于 ...
- [深度学习] 计算机视觉低代码工具Supervision库使用指北
Supervision库是一款出色的Python计算机视觉低代码工具,其设计初衷在于为用户提供一个便捷且高效的接口,用以处理数据集以及直观地展示检测结果.Supervision库的官方开源仓库地址为: ...