python 字符串格式化 ( 百分号 & format )
Python的字符串格式化有两种方式: 百分号方式、format方式
百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存。
----百分号
tpl = "i am %s" % "alex"
#i am alex tpl = "i am %s age %d" % ("alex", 18)
#i am alex age 18 tpl = "i am %(name)s age %(age)d" % {"name": "alex", "age": 18}
#i am alex age 18 tpl = "percent %.2f" % 99.97623
#percent 99.98 tpl = "i am %(pp).2f %%" % {"pp": 123.425556, }
#i am 123.43 %
----format
tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
#i am seven, age 18, alex
tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
#i am seven, age 18, alex
tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
#i am seven, age 18, really seven
tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
#i am seven, age 18, really seven
tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
#i am seven, age 18, really seven
tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
#i am seven, age 18, really seven
tpl = "i am {0[0]}, age {1[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
#i am 1, age 22, really 3
tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
#i am seven, age 18, money 88888.100000
tpl = "i am {:s}, age {:d}".format(*["seven", 18])
#i am seven, age 18
tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
#i am seven, age 18
tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
#i am seven, age 18
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
#numbers: 1111,17,15,f,F, 1587.623000%
tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
#numbers: 1111,17,15,f,F, 1587.623000%
tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
#numbers: 1111,17,15,f,F, 1500.000000%
tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
#numbers: 1111,17,15,f,F, 1500.000000%
import math
print('The value of PI is approximately {}.'.format(math.pi))
#The value of PI is approximately 3.14159265359.
print('The value of PI is approximately {!r}.'.format(math.pi))
#The value of PI is approximately 3.141592653589793.
print('The value of PI is approximately {0:.3f}.'.format(math.pi))
#The value of PI is approximately 3.142.
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print('{0:10} ==> {1:10d}'.format(name, phone))
'''
Dcab ==> 7678
Jack ==> 4098
Sjoerd ==> 4127
'''
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))
#Jack: 4098; Sjoerd: 4127; Dcab: 8637678
python 字符串格式化 ( 百分号 & format )的更多相关文章
- python字符串格式化方法 format函数的使用
python从2.6开始支持format,新的更加容易读懂的字符串格式化方法, 从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序 ...
- python字符串格式化之format
用法: 它通过{}和:来代替传统%方式 1.使用位置参数 要点:从以下例子可以看出位置参数不受顺序约束,且可以为{},只要format里有相对应的参数值即可,参数索引从0开,传入位置参数列表可用*列表 ...
- (转)Python 字符串格式化 str.format 简介
原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...
- 一文秒懂!Python字符串格式化之format方法详解
format是字符串内嵌的一个方法,用于格式化字符串.以大括号{}来标明被替换的字符串,一定程度上与%目的一致.但在某些方面更加的方便 1.基本用法 1.按照{}的顺序依次匹配括号中的值 s = &q ...
- Python 字符串格式化
Python 字符串格式化 Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存 一 ...
- Python学习:12.Python字符串格式化
字符串格式化 讲解Python这么久,也没有讲解Python的字符串的格式化,那我们今天就来了解一下python字符串格式化的强大之处. 首先我们先理解一下为什么要有字符串的格式化,就是为了方便字符串 ...
- python字符串格式化 %操作符 {}操作符---总结
Python字符串格式化 (%占位操作符) 在许多编程语言中都包含有格式化字符串的功能,比如C和Fortran语言中的格式化输入输出.Python中内置有对字符串进行格式化的操作 %. 模板 格式化字 ...
- 【转】Python字符串格式化
Python 支持格式化字符串的输出 .尽管这样可能会用到非常复杂的表达式,但最基本的用法是将一个值插入到一个有字符串格式符 %s 的字符串中. 在 Python 中,字符串格式化使用与 C 中 sp ...
- 7. python 字符串格式化方法(2)
7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...
随机推荐
- IOS开发使用委托delegate在不同窗口之间传递数据
IOS开发使用委托delegate在不同窗口之间传递数据是本文要介绍的内容,主要是来讲解如何使用委托delegate在不同窗口之间传递数据,具体内容来看详细内容.在IOS开发里两个UIView窗口之间 ...
- asp.net 视图引擎归类
1. ASPX View Engine 第一个也是我们最熟悉的---aspx,相信做过WebForm开发对Aspx都比较了解: 小示例: <%@ Control Inherits="S ...
- ajax asynx:false
默认设置下,所有请求均为异步请求.如果需要发送同步请求,请将此选项设置为 false.注意,同步请求将锁住浏览器,用户其它操作必须等待请求完成才可以执行 (默认: true) 默认设置下,所有请求均为 ...
- [Android 新特性] 谷歌发布Android Studio开发工具1.0正式版(组图) 2014-12-09 09:35:40
Android Studio是谷歌于13年I/O大会推出的Android开发环境,基于IntelliJ IDEA. 类似 Eclipse ADT,Android Studio 提供了集成的Androi ...
- 函数指针&指针函数
https://blog.csdn.net/luoyayun361/article/details/80428882
- AJAX && JSON之讲解
Ajax技术的核心是XMLHttpRequest对象(简称XHR),可以通过使用XHR对象获取到服务器的数据,然后再通过DOM将数据插入到页面中呈现.虽然名字中包含XML,但Ajax通讯与数据格式无关 ...
- service 和 Controller 差别
service 层能够看做是还有一个 DAO 层,仅仅是在里面封装了还有一些逻辑. 而 Controller 和 service 差别就大了.Controller 要处理请求映射, service ...
- Laravel 学习 .env文件 getenv 获得环境变量的值
Laravel 学习 .env文件 getenv 获得环境变量的值 我们还需要对应用的 .env 文件进行设置,为应用指定数据库名称 sample. .env . . . DB_DATABASE=s ...
- grid 布局一 固定宽度+自适应宽度
<!DOCTYPE html> <html lang="zh"> <head> <meta charset="UTF-8&quo ...
- PHP怎么实现站点保存快捷方式
PHP怎么实现站点保存快捷方式 <?php $Shortcut = "[InternetShortcut] URL=http://blog.csdn.net/phpfenghuo/ I ...