python format格式化函数用法
python format格式化函数用法
Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。
基本语法是通过 {} 和 : 来代替以前的 % 。
format 函数可以接受不限个参数,位置可以不按顺序。
1.使用位置参数
>>>"{} {}".format("hello", "world") # 不设置指定位置,按默认顺序
'hello world'
>>> "{0} {1}".format("hello", "world") # 设置指定位置
'hello world'
>>> "{1} {0} {1}".format("hello", "world") # 设置指定位置
'world hello world'
2.使用关键字参数
要点:关键字参数值要对得上,另外可以通过字典和列表索引设置参数。
#!/usr/bin/python
# -*- coding: UTF-8 -*- print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com")) # 通过字典设置参数
site = {"name": "菜鸟教程", "url": "www.runoob.com"}
print("网站名:{name}, 地址 {url}".format(**site)) # 通过列表索引设置参数
my_list = ['菜鸟教程', 'www.runoob.com']
print("网站名:{0[0]}, 地址 {0[1]}".format(my_list)) # "0" 是必须的
输出结果:
网站名:菜鸟教程, 地址 www.runoob.com
网站名:菜鸟教程, 地址 www.runoob.com
网站名:菜鸟教程, 地址 www.runoob.com
3.传入对象
#!/usr/bin/python
# -*- coding: UTF-8 -*- class AssignValue(object):
def __init__(self, value):
self.value = value
my_value = AssignValue(6)
print('value 为: {0.value}'.format(my_value)) # "0" 是可选的
输出结果:
value 为: 6
4.格式化
>>> print("{:.2f}".format(3.1415926));
3.14
| 数字 | 格式 | 输出 | 描述 |
|---|---|---|---|
| 3.1415926 | {:.2f} | 3.14 | 保留小数点后两位 |
| 3.1415926 | {:+.2f} | +3.14 | 带符号保留小数点后两位 |
| -1 | {:+.2f} | -1.00 | 带符号保留小数点后两位 |
| 2.71828 | {:.0f} | 3 | 不带小数 |
| 5 | {:0>2d} | 05 | 数字补零 (填充左边, 宽度为2) |
| 5 | {:x<4d} | 5xxx | 数字补x (填充右边, 宽度为4) |
| 10 | {:x<4d} | 10xx | 数字补x (填充右边, 宽度为4) |
| 1000000 | {:,} | 1,000,000 | 以逗号分隔的数字格式 |
| 0.25 | {:.2%} | 25.00% | 百分比格式 |
| 1000000000 | {:.2e} | 1.00e+09 | 指数记法 |
| 13 | {:10d} | 13 | 右对齐 (默认, 宽度为10) |
| 13 | {:<10d} | 13 | 左对齐 (宽度为10) |
| 13 | {:^10d} | 13 | 中间对齐 (宽度为10) |
| 11 |
'{:b}'.format(11)
|
1011 |
进制 |
^, <, > 分别是居中、左对齐、右对齐,后面带宽度, : 号后面带填充的字符,只能是一个字符,不指定则默认是用空格填充。
+ 表示在正数前显示 +,负数前显示 -; (空格)表示在正数前加空格
b、d、o、x 分别是二进制、十进制、八进制、十六进制。
此外我们可以使用大括号 {} 来转义大括号,如下实例:
#!/usr/bin/python
# -*- coding: UTF-8 -*- print ("{} 对应的位置是 {{0}}".format("runoob"))
输出结果为:
runoob 对应的位置是 {0}
python format格式化函数用法的更多相关文章
- 【387】Python format 格式化函数
参考:Python format 格式化函数 # 保留小数点后两位 f'{3.1415926:.2f}' # 带符号保留小数点后两位 f'{3.1415926:+.2f}' f'{-1:+.2f}' ...
- Python format 格式化函数。
Python format 格式化函数 Python 字符串 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 ...
- 【Python】Python format 格式化函数(转帖)
https://www.runoob.com/python/att-string-format.html Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符 ...
- Python format格式化函数
参考资料:https://www.runoob.com/python/att-string-format.html 在学习Python的时候碰到了一个很有趣的格式化输入的技巧,下面记录在此. Pyth ...
- Python format 格式化函数
str.format() 格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % format 函数可以接受不限个参数,位置可以不按 ...
- Python format 格式化函数 格式化字符串
- 【313】python 中 print 函数用法总结
参考:python 中 print 函数用法总结 参考:Python print() 函数(菜鸟教程) 参考:Python 3 print 函数用法总结 目录: 字符串和数值类型 变量 格式化输出 p ...
- Python 3 print 函数用法总结
Python 3 print 函数用法总结 1. 输出字符串和数字 print("runoob") # 输出字符串 runoob print(100) ...
- 细说Python的lambda函数用法,建议收藏
细说Python的lambda函数用法,建议收藏 在Python中有两种函数,一种是def定义的函数,另一种是lambda函数,也就是大家常说的匿名函数.今天我就和大家聊聊lambda函数,在Pyth ...
随机推荐
- C++入门经典-例2.4-使用scanf格式输入函数得到用户输入的数据
1:puts函数可以输出提示信息的字符串. 2:代码如下: // 2.4.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" int main( ...
- JS 由前端保存到文件
function doSave(value, type, name) { var blob; if (typeof window.Blob == "function") { blo ...
- C#操作WMI文章汇总
http://blog.csdn.net/linux7985/article/details/5698932 http://www.cnblogs.com/ocean2000/archive/2008 ...
- leetcode-easy-string-242. Valid Anagram
mycode 71.97% class Solution(object): def isAnagram(self, s, t): """ :type s: str : ...
- SQL Server AlwaysOn原理简介
SQL Server2012所支持的AlwaysOn技术集中了故障转移群集.数据库镜像和日志传送三者的优点,但又不相同.故障转移群集的单位是SQL实例,数据库镜像和日志传送的单位是单个用户数据库,而A ...
- 关于ArrayList的越界问题?
大家都知道 ArrayList是自动扩容的. 那为什么会存在越界问题? 话不多说 上代码 package test; import java.util.ArrayList; public class ...
- Java: Integer用==比较时127相等128不相等的原因
直接看问题吧 for (int i = 0; i < 150; i++) { Integer a = i; Integer b = i; System.out.println(i + " ...
- CircleCi 不更新某个分支的两种方法
概述 今天我发现我的所有项目的 CircleCi 部署全部都会更新 gh-pages 分支.找了好久,终于找到了不更新的方法.于是我总结了一下,记录下来,供以后开发时参考,相信对其他人也有用. onl ...
- 远程连接elasticsearch遇到的问题
本文转自:https://blog.csdn.net/xuchuangqi/article/details/78989940 1.首先要远程连接就要把配置文件的network.host: 改为 net ...
- RabbitMQ问题解决
1.访问http://localhost:15672/#/,输入用户名.密码登录报错500 解决方法:在快捷程序处找到RabbitMQ Service -stop停止服务,然后再点击RabbitMQ ...