关于python中format占位符中的 {!} 参数
2024-10-20 01:37:10
在看celery的时候,发现里面有这么一句
print('Request: {0!r}'.format(self.request))
关于里面的{0!r}
是什么意思翻了一下文档。
文档里是这么描述的
replacement_field ::= "{" [field_name] ["!" conversion] [":" format_spec] "}"
------
Two conversion flags are currently supported: '!s' which calls str() on the value, and '!r' which calls repr().
Some examples:
"Harold's a clever {0!s}" # Calls str() on the argument first
"Bring out the holy {name!r}" # Calls repr() on the argument first
是说感叹号后面跟的是conversion,而conversion有两个值.
分别是s对应str()函数, r对应repr()函数。
因此上面的翻译一下类似于下面
"Harold's a clever {0!s}" == "Harold's a clever str({0})"
"Bring out the holy {name!r}" == "Bring out the holy repr({name})"
实际调用时的写法应该是
"Harold's a clever {0!s}".format(string) == "Harold's a clever {0}".format(str(string))
"Bring out the holy {name!r}".format(string) == "Bring out the holy {name}".format(repr(string))
最新文章
- Dao跨事务调用实现转账功能
- Maven学习-目录结构
- Java产生随机数
- android图像与图像处理系列(一、Bitmap和BitmapFactory)
- 查询各个商品分类中各有多少商品的SQL语句
- C语言之实现控制台光标随意移动
- 共享内存shared pool (4):Library cache 转储文件
- nginx笔记---http配置
- android78 Fragment和Activity 传递数据
- 在javaweb中通过servlet类和普通类读取资源文件
- LINUX进程上锁查看方法
- hdu 1564 Play a game(博弈找规律)
- calss 类
- rsa加密解密, 非对称加密
- Servlet基本_セッション属性
- jquery实现元素高度变化页面变化
- [Windows Azure] Using the Graph API to Query Windows Azure AD
- 多媒体文件格式之RMVB
- WPF学习笔记-用Expression Design制作矢量图然后导出为XAML
- MySql 模糊查询、范围查询