#使用str.format()函数

#使用'{}'占位符
print('I\'m {},{}'.format('Hongten','Welcome to my space!'))
>>> I'm 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'))

>>> Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com

#可以改变占位符的位置
print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello','hongtenzone@foxmail.com'))
>>> Hello,I'm Hongten,my E-mail is hongtenzone@foxmail.com
print('#' * 40)
########################################
#使用'{name}'形式的占位符
print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?'))
>>> Hi,Tom,How old are you?
print('#' * 40)
########################################
#混合使用'{0}','{name}'形式
print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!'))
>>> Hello,I'm Hongten,This is a test message!
print('#' * 40)
########################################
#下面进行格式控制
import math
print('The value of PI is approximately {}.'.format(math.pi))

>>> The value of PI is approximately 3.141592653589793.

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

str.format的更多相关文章

  1. python str.format()

    python中的字符串格式函数str.format(): #使用str.format()函数 #使用'{}'占位符 print('I\'m {},{}'.format('Hongten','Welco ...

  2. python开发_python中str.format()

    格式化一个字符串的输出结果,我们在很多地方都可以看到,如:c/c++中都有见过 下面看看python中的字符串格式函数str.format(): 1 #使用str.format()函数 2 3 #使用 ...

  3. #python str.format 方法被用于字符串的格式化输出。

    #python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...

  4. Python3基础 str format 位置参数与关键字参数

             Python : 3.7.0          OS : Ubuntu 18.04.1 LTS         IDE : PyCharm 2018.2.4       Conda ...

  5. str.format格式化用法(通过{}来替代%)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #str.format格式化用法(通过{}来替代%) ''' >>> help(format ...

  6. 【转】Python格式化字符串str.format()

    原文地址:http://blog.xiayf.cn/2013/01/26/python-string-format/ 每次使用Python的格式字符串(string formatter),2.7及以上 ...

  7. (转)Python 字符串格式化 str.format 简介

    原文:https://www.cnblogs.com/wilber2013/p/4641616.html http://blog.konghy.cn/2016/11/25/python-str-for ...

  8. Python-字符串处理 str.format()

    Python中内置的%操作符可用于格式化字符串操作,控制字符串的呈现格式.Python中还有其他的格式化字符串的方式,但%操作符的使用是最方便的. 另外python还有一个更强大的字符串处理函数 st ...

  9. Python——format()/str.format()函数

    格式化输出,除了类似于C语言的格式化输出外,还有str.format()方法,Python内建的format()函数,允许用户将待输出值以参数的形式,调用format()函数,在Python交互式sh ...

随机推荐

  1. 嵌入式Linux驱动学习之路(二十二)用内存模拟磁盘

    安装驱动后,可在/dev/目录下发现已经生成了相应的设备文件. 格式化设备:mkdosfs /dev/ramblock. 挂载设备. 读写设备 . 驱动程序代码: /***************** ...

  2. Apache配置手札

    一.绑定域名到子目录 在httpd.conf文件末尾添加 #不同的域名对应到的目录 <VirtualHost *:80> DocumentRoot "D:\wamp\www\ba ...

  3. 链队列java实现

    public class LinkHeap<T> { class Node<T> { T data; Node<T> next; Node(T data) { th ...

  4. python爬某个网站的图片

    # _*_ coding: gbk _*_ import urllib import urllib2 import re class Spider: def getImage(self,html): ...

  5. Python3 基本数据类型

    Python中的变量不需要声明,每个变量使用前必须赋值,变量赋值后才会被创建,在Python中变量就是变量,它没有类型.我们所说的"类型"是变量所指的内存中对象的类型. 等号(=) ...

  6. Pycharm 输出中文或打印中文乱码现象的解决办法

    1. 确保文件开头加上以下代码: # -*- coding:utf-8 -*- 还可以加上 import sys reload(sys) sys.setdefaultencoding('utf-8') ...

  7. cocos2d-x打飞机实例总结

    写了一个cocos2d-x的打飞机游戏,为了深入了解,准备进入引擎内部,深入分析一下打飞机,顺便梳理一下相关的知识 打算分为几个部分: 1.程序入口和场景切换模块分析:简单了解HelloWorld怎样 ...

  8. TypeScript Type Innference(类型推断)

    在这一节,我们将介绍TypeScript中的类型推断.我们将会讨论类型推断需要在何处用到以及如何推断. 基础 在TypeScript中,在几个没有明确指定类型注释的地方将会使用类型推断来提供类型信息. ...

  9. 100_1小记ressons analysis

    想到一首诗用以自勉: look to the master, follow the master, walk with the master, see through the master, beco ...

  10. 优化Table View

    优化Table View Table view需要有很好的滚动性能,不然用户会在滚动过程中发现动画的瑕疵. 为了保证table view平滑滚动,确保你采取了以下的措施: 正确使用`reuseIden ...