python str.format()
python中的字符串格式函数str.format():
#使用str.format()函数
#使用'{}'占位符
print('I\'m {},{}'.format('Hongten','Welcome to my space!'))
print('#' * 40)
#也可以使用'{0}','{1}'形式的占位符
print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten','hongtenzone@foxmail.com'))
#可以改变占位符的位置
print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello','hongtenzone@foxmail.com'))
print('#' * 40)
#使用'{name}'形式的占位符
print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?'))
print('#' * 40)
#混合使用'{0}','{name}'形式
print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!'))
print('#' * 40)
#下面进行格式控制
import math
print('The value of PI is approximately {}.'.format(math.pi))
print('The value of PI is approximately {!r}.'.format(math.pi))
print('The value of PI is approximately {0:.3f}.'.format(math.pi))
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
print('{0:10} ==> {1:10d}'.format(name, phone))
table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))
代码
Python 3.3.2 (v3.3.2:d047928ae3f6, May 16 2013, 00:03:43) [MSC v.1600 32 bit (Intel)] on win32
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
I'm Hongten,Welcome to my space!
########################################
Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com
Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com
########################################
Hi,Tom,How old are you?
########################################
Hello,I'm Hongten,This is a test message!
########################################
The value of PI is approximately 3.141592653589793.
The value of PI is approximately 3.141592653589793.
The value of PI is approximately 3.142.
Jack ==> 4098
Sjoerd ==> 4127
Dcab ==> 7678
Jack: 4098; Sjoerd: 4127; Dcab: 8637678
>>>
运行结果
python str.format()的更多相关文章
- #python str.format 方法被用于字符串的格式化输出。
#python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...
- python str.format 中文对齐的细节问题
写了一个练手的爬虫...在输出的时候出现了让人很不愉♂悦的问题 像这样: 令人十分难受啊! #----------------------------------------------------- ...
- python开发_python中str.format()
格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...
- 【转】Python格式化字符串str.format()
原文地址:http://blog.xiayf.cn/2013/01/26/python-string-format/ 每次使用Python的格式字符串(string formatter),2.7及以上 ...
- (转)Python 字符串格式化 str.format 简介
原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...
- Python——format()/str.format()函数
格式化输出,除了类似于C语言的格式化输出外,还有str.format()方法,Python内建的format()函数,允许用户将待输出值以参数的形式,调用format()函数,在Python交互式sh ...
- Python中用format函数格式化字符串
Python的字符串格式化有两种方式: 百分号方式.format方式 百分号的方式相对来说比较老,而format方式则是比较先进的方式,企图替换古老的方式,目前两者并存. 1.百分号方式 语法:%[( ...
- Python中用format函数格式化字符串的用法
这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下 自python2. ...
- Python中format的用法
自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱.语法 它通过{}和: ...
随机推荐
- 使用EntityFramework中DbSet.Set(Type entityType)方法碰到的问题
使用的是EntityFramework, Version=6.0.0.0,项目原本直接使用将EntityFramework的Entity拿到UI使用,后面想使用dto对象将数据库的Entity与前台分 ...
- [TPYBoard-Micropython之会python就能做硬件 7] 学习使用蓝牙模块及舵机
转载请注明:@小五义 http://www.cnblogs.com/xiaowuyi 欢迎加入讨论群 64770604 一.实验器材 1.TPYboard V102板 一块 2 ...
- 第25篇 jQuer快速学习(上)---选择器和DOM操作
这个文章经历的时间比较长,不是因为jQuery比较难,而是东西比较多,真心是个体力活.所以本来想把jQuery做成一篇去写,但由于写的时候发现jQuery发现写成一篇的话过于长,对于阅读起来也不是一个 ...
- JMessage是让App 同时集成 Push 功能与 IM 功能最完美的方案
历经几个月的沉寂,以及兄弟们的奋战,极光推送的兄弟产品诞生了:极光IM,英文名 JMessage. 极光IM 是我们团队基于大量客户的需求反馈,在很多客户的殷切期盼下所开发的.团队成员一方面要支撑极光 ...
- Linux i2c子系统(三) _解决probe无法执行
如果你也遇到了填充了id_match_table,compitible怎么看都一样,但probe就是不执行(让我哭一会),你可以回头看一下上一篇的模板,我们这里虽然使用的是设备树匹配,但和platfo ...
- .NET获取客户端的操作系统、IP地址、浏览器版本
获取客户端的操作系统: #region 获取操作系统版本号 /// <summary> /// 获取操作系统版本号 /// </summary> /// <returns ...
- wemall开源商城免费商城系统部分代码(内含代码地址)
wemall开源商城免费商城系统部分代码,下面分享部分代码,供学习者学习: 开源版把install文件夹下的install.lock删除之后可进行自动安装 后台访问地址:http:// www.xxx ...
- Hibernate优缺点
下面就Hibernate优缺点分别进行简单的阐述.1.Hibernate优点:(1)对象/关系数据库映射(Basic O/R Mapping)它使用时只需要操纵对象,使开发更对象化,抛弃了数据库中心的 ...
- Java向上转型的意义
比如我这个程序 public class Testjava{ public static void main(String args[]) { fun(new Student()); fun(new ...
- 超级素数幂 java
链接:https://www.nowcoder.com/questionTerminal/fb511c3f1ac447309368d7e5432c6c79来源:牛客网如果一个数字能表示为p^q(^表示 ...