格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过

下面看看python中的字符串格式函数str.format():

 1 #使用str.format()函数
2
3 #使用'{}'占位符
4 print('I\'m {},{}'.format('Hongten','Welcome to my space!'))
5
6 print('#' * 40)
7
8 #也可以使用'{0}','{1}'形式的占位符
9 print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten','hongtenzone@foxmail.com'))
10 #可以改变占位符的位置
11 print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello','hongtenzone@foxmail.com'))
12
13 print('#' * 40)
14
15 #使用'{name}'形式的占位符
16 print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?'))
17
18 print('#' * 40)
19
20 #混合使用'{0}','{name}'形式
21 print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!'))
22
23 print('#' * 40)
24
25 #下面进行格式控制
26 import math
27 print('The value of PI is approximately {}.'.format(math.pi))
28 print('The value of PI is approximately {!r}.'.format(math.pi))
29 print('The value of PI is approximately {0:.3f}.'.format(math.pi))
30
31
32 table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
33 for name, phone in table.items():
34 print('{0:10} ==> {1:10d}'.format(name, phone))
35
36
37 table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
38 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

python开发_python中str.format()的更多相关文章

  1. python开发_python中的Boolean运算和真假值

    python中的真假值: Truth Value Testing Any object can be tested for truth value, for use in an if or while ...

  2. python开发_python中字符串string操作

    在python中,对于字符串string的操作,我们有必要了解一下,这样在我们的以后的开发中会给我们带来很多方便 下面是我学习的笔记: #python-string #python中的字符串用单引号' ...

  3. python开发_python中的list操作

    对python中list的操作,大家可以参考: Python list 操作 以下是我个人的笔记: ============================================ Add b ...

  4. python开发_python中的range()函数

    python中的range()函数的功能hen强大,所以我觉得很有必要和大家分享一下 就好像其API中所描述的: If you do need to iterate over a sequence o ...

  5. python开发_python中的module

    在python中,我们可以把一些功能模块化,就有一点类似于java中,把一些功能相关或者相同的代码放到一起,这样我们需要用的时候,就可以直接调用了 这样做的好处: 1,只要写好了一个功能模块,就可以在 ...

  6. python开发_python中的函数定义

    下面是我做的几个用列: #python中的函数定义,使用和传参 def_str = '''\ python中的函数以如下形式声明: def 函数名称([参数1,参数2,参数3......]): 执行语 ...

  7. python开发_python中的变量:全局变量和局部变量

    如果你在为python中的变量:全局变量和局部变量头疼,我想这篇blog会给你帮助 运行效果: 代码部分: #Python中的变量:全局变量和局部变量 #在很多语言中,在声明全局变量的时候,都喜欢把全 ...

  8. python开发_python中for循环操作

    如果你对python中的for循环不是很清楚,请看看这篇文章:”for循环控制语句——菜鸟的Python笔记“ 下面是我做的一些学习记录供大家参考: #基本的for循环语句 test_list = [ ...

  9. python开发_python关键字

    python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...

随机推荐

  1. dojo处理删除操作报错

    1.错误描写叙述    java.lang.IllegalArgumentException:attempt to create delete event with null entity. 2.错误 ...

  2. bzoj1513【POI2006】Tet-Tetris 3D

    1513: [POI2006]Tet-Tetris 3D Time Limit: 30 Sec  Memory Limit: 162 MB Submit: 733  Solved: 245 [Subm ...

  3. 【Jquery系列】之DOM属性

    1   概述 本章将结合JQuery官方API,对Jquery属性进行分析与讲解.主要讲.addClass(),.attr(),,hasClass(),,html(),.prop(),.removeA ...

  4. Python笔记·第五章—— 列表(List) 的增删改查及其他方法

    一.列表的简介   列表是python中的基础数据类型之一,其他语言中也有类似于列表的数据类型,比如js中叫数组,他是以[ ]括起来,每个元素以逗号隔开,而且他里面可以存放各种数据类型比如:li = ...

  5. java.net.BindException: Cannot assign requested address: bind

    异常信息 时间:2017-03-16 10:21:05,644 - 级别:[ERROR] - 消息: [other] Failed to start end point associated with ...

  6. spring cloud sidecar

    用spring cloud sidecar的整合异构语言,以前做过没有做笔记,现在再做由于各种坑又浪费了一天,这里记一下 首先是官网:http://cloud.spring.io/spring-clo ...

  7. Kafka详细的设计和生态系统

    欢迎大家前往云加社区,获取更多腾讯海量技术实践干货哦~ 译者:人工智能资讯小编 本译文自Jean-Paul Azar 在 https://dzone.com 发表的 Kafka Detailed De ...

  8. spring boot入门 -- 介绍和第一个例子

    "越来越多的企业选择使用spring boot 开发系统,spring boot牛在什么地方?难不难学?心动不如行动,让我们一起开始学习吧!" 使用Spring boot ,可以轻 ...

  9. php项目报错 Warning: session_start(): open(D:/software/wamp/wamp/tmp\sess_msrjot7f32ciqb1p2hr4ahejg4, O_RDWR) f

    今天一个php项目报错: Warning: session_start(): open(D:/software/wamp/wamp/tmp\sess_msrjot7f32ciqb1p2hr4ahejg ...

  10. bzoj 1758: [Wc2010]重建计划

    Description Input 第 一行包含一个正整数N,表示X国的城市个数. 第二行包含两个正整数L和U,表示政策要求的第一期重建方案中修建道路数的上下限 接下来的N-1行描述重建小组的原有方案 ...