python format
python自2.6后,新增了一种格式化字符串函数str.format(),威力十足,可以替换掉原来的%
注:以下操作版本是python2.7
映射示例
语法
通过{} 和 : 替换 %
通过位置
>>> '{0} is {1}'.format('jihite', '4 years old')
'jihite is 4 years old'
>>> '{0} is {1} {0}'.format('jihite', '4 years old')
'jihite is 4 years old jihite'
通过format函数可以接受不限参数个数、不限顺序
通过关键字
>>> '{name}:{age}'.format(age=4,name='jihite')
'jihite:4'
>>> '{name}:{age}'.format(age=4,name='jihite',locate='Beijing')
'jihite:4'
format括号内用=给变量赋值
通过对象属性
>>> class Person:
... def __init__(self, name, age):
... self.name,self.age = name, age
... def __func__(self):
... return "This guy is {self.name}, is {self.age} old".format(self=self)
...
>>> s =Person('jihite', 4)
>>> s.__func__()
'This guy is jihite, is 4 old'
通过下标
>>> '{0[0]} is {0[1]} years old!'.format(['jihite', 4])
'jihite is 4 years old!'
>>> '{0} is {1} years old!'.format('jihite', 4)
'jihite is 4 years old!'
其实就是通过位置
格式限定符
通过{} : 符号
填充和对齐
^<>分别表示居中、左对齐、右对齐,后面带宽度
>>> '{:>10}'.format('jihite')
' jihite'
>>> '{:<10}'.format('jihite')
'jihite '
>>> '{:^10}'.format('jihite')
' jihite '
精度和类型f
精度常和f一起使用
>>> '{:.2f}'.format(3.1415)
'3.14'
>>> '{:.4f}'.format(3.1)
'3.1000'
进制转化
>>> '{:b}'.format(10)
''
>>> '{:o}'.format(10)
''
>>> '{:d}'.format(10)
''
>>> '{:x}'.format(10)
'a'
其中b o d x分别表示二、八、十、十六进制
千位分隔符
>>> '{:,}'.format(1000000)
'1,000,000'
>>> '{:,}'.format(100000.23433)
'100,000.23433'
>>> '{:,}'.format('abcedef')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: Cannot specify ',' with 's'.
这种情况只针对数字
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 format()函数的用法
Python format() 函数的用法 复制自博主 chunlaipiupiupiu 的博客,如有侵权,请联系删除 python中format函数用于字符串的格式化 通过关键字 1 print(' ...
- python format 用法详解
format 用法详解 不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 单个参数可以多次输出,参数顺序可以不相同 填充方式十分灵活,对齐方式十分强大 官方推荐用的方式,%方式将会在后面的版 ...
- python format函数/print 函数详细讲解(4)
在python开发过程中,print函数和format函数使用场景特别多,下面分别详细讲解两个函数的用法. 一.print函数 print翻译为中文指打印,在python中能直接输出到控制台,我们可以 ...
- python format格式化函数用法
python format格式化函数用法 原文 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前 ...
- Python format格式化输出
http://www.jb51.net/article/63672.htm 推荐参考 >>> '{0},{1}'.format('hello','python') 'hello,py ...
- Python format 格式化函数
str.format() 格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前的 % format 函数可以接受不限个参数,位置可以不按 ...
- python format() 函数
转载 https://www.cnblogs.com/wushuaishuai/p/7687728.html 正文 Python2.6 开始,新增了一种格式化字符串的函数 format() ,它增强了 ...
随机推荐
- ImageMagick jmagick 安装
在安装ImageMagick之前,请检查下面包已经安装 tiff-3.9.5.tar.gz (rpm -qa|grep libtiff检查是否已经安装) libpng-1.2.46.t ...
- ASP.NET MVC4系列验证机制、伙伴类共享源数据信息(数据注解和验证)
一,mvc前后台验证 自定义属性标签MyRegularExpression using System; using System.Collections.Generic; using System.C ...
- 【翻译】西川善司的「实验做出的游戏图形」「GUILTY GEAR Xrd -SIGN-」中实现的「纯卡通动画的实时3D图形」的秘密,后篇
http://www.4gamer.net/games/216/G021678/20140714079/ 连载第2回的本回, Arc System Works开发的格斗游戏「GUILTY G ...
- 【翻译】CEDEC2013 BANDAI NAMCO 了解游戏格斗动画中的身体运动结构和原理
CEDEC搬运工程开始~ 这篇会议PPT的作者 元梅幸司曾经就职在TECMO参与开发了死或生2,3[ DEAD OR ALIVE],忍龙「NINJA GAIDEN」后来加入NAMCO(现在是BAN ...
- Unity3d中C#使用指针(Unsafe)的办法(转)
近日由于在U3D项目中要使用到数据传递(C++ DLL的数据传递给U3D中的C#),其中涉及到需要使用C#的指针.直接编译会出现以下错误Unsafe code requires the 'unsafe ...
- 语艺杂谈1 – MAP赋值与插入
MAP赋值和插入,对于相同ID的处理方式不同,前者为替换 后者为插入失败 #include <map> #include <string> #include <iostr ...
- ServletDemo
1. Servlet 接口 继承 Servlet 接口,实现Servlet 接口的 所有抽象方法! 实现类代码 package xw.servlet; import java.io.IOExcepti ...
- vimtutor
================================================================================ 欢 迎 阅 读 < V I M ...
- NRF51822之app_button使用
我们现在开始使用app_button,为什么要使用这个来替代直接使用GPIOTE呢? 因为我们在手册中可以看到如果一直开启GPIOTE in模式的需要需要很大电流.所以我们需要使用RTC来“周期”的查 ...
- lua环境安装 转
curl -R -O http://www.lua.org/ftp/lua-5.2.2.tar.gz tar zxf lua-5.2.2.tar.gz cd lua-5.2.2 make linux ...