使用 {}.format 对字符串进行格式(一)
语法格式:
{ [field_name] ["!" conversion] [":" format_spec] }.format()
各选项的详细解释:
field_name ::= arg_name ("." attribute_name | "[" element_index "]")*arg_name ::= [identifier | digit+]attribute_name ::= identifierelement_index ::= digit+ | index_stringindex_string ::= <any source character except "]"> +conversion ::= "r" | "s" | "a"format_spec ::= [[fill]align][sign][#][0][width][grouping_option][.precision][type]fill ::= <any character>align ::= "<" | ">" | "=" | "^"sign ::= "+" | "-" | " "width ::= digit+grouping_option ::= "_" | ","precision ::= digit+type ::= "b" | "c" | "d" | "e" | "E" | "f" | "F" | "g" | "G" | "n" | "o" | "s" | "x" | "X" | "%"
具体案例:
1、使用参数的索引号作为 field_name
'{0}'.format("ab", 1, "cd") # 输出:'ab'。对第一个参数进行格式化# 如果 field_name 为空(即不写参数的索引号),则依次代表0,1,2...
"From {} to {}".format("A", "B", "c", 1) # 输出:'From A to B'。等价于 "From {0} to {1}".format("A", "B", "c", 1)
2、使用参数的名字作为 field_name
"My quest is {A}".format(B='what', A='that') # 输出:'My quest is that' """
注意:当 format 的参数以元组形式呈现的时候,只能用参数的索引作为 field_name;当 format 的参数以字典形式呈现的时候,只能用参数的名字作为 field_name。
以下两种写法均会报错
"""
# "My quest is {1}".format(B='what', A='that') # 将会报错:IndexError: Replacement index 1 out of range for positional args tuple
# "My quest is {what}".format('what', 'that') # 将会报错:KeyError: 'what'"""但是,如果参数是 元组+字典形式"""
"My quest is {}".format('what', A='that') # 输出:'My quest is what'
"My quest is {A}".format('what', A='that') # 输出:'My quest is that'
3、使用参数的属性作为 field_name
class Test:def __init__(self):self.name = "老王"self.age = 47test = Test()
"This test's name={0.name} , age={0.age}".format(test) # 输出:"This test's name=老王 , age=47""""但是,下面这种写法会报错"""
# "This test's name={test.name} , age={test.age}".format(test) # 报错:KeyError: 'test'
# 应该写为:
"This test's name={test.name} , age={test.age}".format(test=test) # 输出:"This test's name=老王 , age=47"
4、使用参数的切片作为 field_name
"我是隔壁 老{names[2]}".format(names=("李", "刘", "王", "吴")) # 输出:'我是隔壁 老王'
5、三种转换标志:!s(将调用str())、!r(将调用repr())、!a(将调用ascii())
type('{string!s}'.format(string=2)) # 输出:str;而 type(2) = int'this is a {string!r}'.format(string='strings') # 输出:"this is a 'strings'""这是一个{!a}".format("测试") # 输出:"这是一个'\\u6d4b\\u8bd5'"
更多链接:
1、选项 format_spec 的使用
2、str()、repr() 与 ascii() 的区别
使用 {}.format 对字符串进行格式(一)相关推荐
- C#中string.format用法详解 格式字符串
string.Format对C#字符串格式化 String.Format 方法的几种定义: String.Format (String, Object) 将指定的 String 中的格式项替换为指定的 ...
- Java学习笔记(七)--格式化字符串及格式输出
一.格式化字符串 String 类的静态 format() 方法用于创建格式化的字符串. 1.format(String format, Object···args) 该方法使用指定的格式字符串和参数 ...
- C语言strftime()函数 (格式化时间)(注意第二个参数size_t maxsize一定要大于第三个参数const char *format指向字符串的长度)
需包含:C 标准库 - <time.h> 文章目录 描述 声明 参数 返回值 实例 使用☞坑(注意第二个参数size_t maxsize一定要大于第三个参数const char *form ...
- 需求澄清_澄清字符串的格式!
需求澄清 用户友好的介绍 如果您不熟悉String格式或只想快速解决问题,那么String格式的Java文档不是最容易阅读和理解的. 尽管它很完整,但是它不是非常用户友好的,所以我想我会尝试编写一个更 ...
- mysql的时间在cmd的输入格式_获取“System.FormatException:输入字符串的格式不正确。”在日期时间对象插入到MySql数据库...
我正在使用C#与Mvvm Light和Mysql数据库的应用程序.当我做一个插入到数据库与对象是一个DateTime对象,我收到以下错误的参数:获取"System.FormatExcepti ...
- C# Sharp问题解决:System.FormatException 输入字符串的格式不正确
一.遇到的问题 如下代码,突然报错! private void Form1_Load(object sender, EventArgs e) {string uuid = "xxxxx&qu ...
- 串口字符串-HEX格式
介绍 串口通信过程中 通常涉及一个数据的模拟过程以及数据发送过程, 一般来说, 我们会发送一串指令给下位机 68 05 00 84 01 02 03 例如这种, 我们明白 这是我们 将相应的字符转换成 ...
- string.Format 格式化字符串
http://www.cnblogs.com/linyechengwei/archive/2009/11/17/1604509.html 今天在做项目时,碰到一个很奇怪的问题,我使用string.Fo ...
- DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的有效值。 ---> System.FormatException: 输入字符串的格式不正确。
System.FormatException: 输入字符串的格式不正确. 参考文章: (1)DataGridView 中发生以下异常: System.Exception: 是 不是 Decimal 的 ...
- Python基础教程:format 格式化字符串用法
1.format 格式化字符串 (1) 顺序传参 (2) 索引传参 (3) 关键字传参 (4) 容器类型传参 {} 代表的是占位符 2.顺序传参 strvar = "{}向{}开了一枪&qu ...
最新文章
- 认知智能再突破,阿里 18 篇论文入选 AI 顶会 KDD
- CKeditor3.6.2 配置与精简
- 使用CNN做电影评论的负面检测——本质上感觉和ngram或者LSTM同,因为CNN里图像检测卷积一般是3x3,而文本分类的话是直接是一维的3、4、5...
- Java知识点梳理——继承
- Go的50度灰:Golang新开发者要注意的陷阱和常见错误
- linux中管道符的简单使用
- 什么是二叉树?以及二叉树如何遍历?
- Computer Hardware
- Scala学习笔记03:操作符
- Linux应用编程基础01:Linux应用编程绪论
- 数据结构实验之数组一:矩阵转置
- web前端小项目个人实例_Web前端:小程序界面与逻辑项目实训
- 创建一个带有Lookup字段的List
- 1、position用法技巧,2、CSS 属性 选择器,3、CSS 选择器
- Metamask + remix:在ropsten测试链上取出已经部署的合约并进行一些操作
- sklearn kfold_sklearn函数:cross_val_score(交叉验证评分)
- 基于SpringBoot+Vue的音乐网站项目-附源码+报告
- 钢铁雄心II HOI2 作弊码合辑
- React将字符串转化成组件,将Antd图标字符串转化为图标组件
- win7 无法开启启用网络发现
热门文章
- 单片机指令系统(1)
- Spring3.2.1+Hibernate4.1.7 多数据源动态切换
- contextcapture多区块点云_Smart 3D (ContextCapture) 4.4.6版本最新功能
- word文档误删怎么恢复
- UBI文件系统的制作与挂载案例
- 微软官网操作系统下载方法
- regedit命令应用
- 大数据实战第十二课之-Scala知识05
- 女生学计算机专业用考研吗,计算机专业女生考研,该选什么方向比较好?谢谢!...
- 【花瓣网】花瓣网练习-首页的静态页