#通过位置
print '{0},{1}'.format('chuhao',20) print '{},{}'.format('chuhao',20) print '{1},{0},{1}'.format('chuhao',20) #通过关键字参数
print '{name},{age}'.format(age=18,name='chuhao') class Person:
def __init__(self,name,age):
self.name = name
self.age = age def __str__(self):
return 'This guy is {self.name},is {self.age} old'.format(self=self) print str(Person('chuhao',18)) #通过映射 list
a_list = ['chuhao',20,'china']
print 'my name is {0[0]},from {0[2]},age is {0[1]}'.format(a_list)
#my name is chuhao,from china,age is 20 #通过映射 dict
b_dict = {'name':'chuhao','age':20,'province':'shanxi'}
print 'my name is {name}, age is {age},from {province}'.format(**b_dict)
#my name is chuhao, age is 20,from shanxi #填充与对齐
print '{:>8}'.format('189')
# 189
print '{:0>8}'.format('189')
#00000189
print '{:a>8}'.format('189')
#aaaaa189 #精度与类型f
#保留两位小数
print '{:.2f}'.format(321.33345)
#321.33 #用来做金额的千位分隔符
print '{:,}'.format(1234567890)
#1,234,567,890 #其他类型 主要就是进制了,b、d、o、x分别是二进制、十进制、八进制、十六进制。 print '{:b}'.format(18) #二进制 10010
print '{:d}'.format(18) #十进制 18
print '{:o}'.format(18) #八进制 22
print '{:x}'.format(18) #十六进制12

python-format函数的更多相关文章

  1. python format()函数的用法

    Python format() 函数的用法 复制自博主 chunlaipiupiupiu 的博客,如有侵权,请联系删除 python中format函数用于字符串的格式化 通过关键字 1 print(' ...

  2. python format函数/print 函数详细讲解(4)

    在python开发过程中,print函数和format函数使用场景特别多,下面分别详细讲解两个函数的用法. 一.print函数 print翻译为中文指打印,在python中能直接输出到控制台,我们可以 ...

  3. python format() 函数

    转载 https://www.cnblogs.com/wushuaishuai/p/7687728.html 正文 Python2.6 开始,新增了一种格式化字符串的函数 format() ,它增强了 ...

  4. python format函数的使用

    转载自:http://www.cnblogs.com/kaituorensheng/p/5709970.html python自2.6后,新增了一种格式化字符串函数str.format(),威力十足, ...

  5. python - format函数 /class内置format方法

    format函数 # format函数 # 用于字符串格式化 # 基本用法: # 方式一:(位置方式) x = "{0}{1}{2}".format(1,2,3) print('1 ...

  6. 第三章:Python基础の函数和文件操作实战

    本課主題 Set 集合和操作实战 函数介紹和操作实战 参数的深入介绍和操作实战 format 函数操作实战 lambda 表达式介绍 文件操作函数介紹和操作实战 本周作业 Set 集合和操作实战 Se ...

  7. python range函数(42)

    在python中使用最多的除了print函数 就是 for循环 了,那么这里就不得不介绍一下python内置函数range函数! 一.range函数简介 python range函数可创建一个整数列表 ...

  8. 飘逸的python - 增强的格式化字符串format函数

    自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式化字符串相比,有什么优越的存在呢?让我们来揭开它羞答答的面纱. 语法 它通过{}和 ...

  9. Python中用format函数格式化字符串的用法

    这篇文章主要介绍了Python中用format函数格式化字符串的用法,格式化字符串是Python学习当中的基础知识,本文主要针对Python2.7.x版本,需要的朋友可以参考下   自python2. ...

  10. 【python】format函数格式化字符串的用法

    来源:http://www.jb51.net/article/63672.htm 自python2.6开始,新增了一种格式化字符串的函数str.format(),可谓威力十足.那么,他跟之前的%型格式 ...

随机推荐

  1. 下载与安装---tensorflow on linux

    http://wiki.jikexueyuan.com/project/tensorflow-zh/get_started/os_setup.html 你可以使用我们提供的 Pip, Docker, ...

  2. Openlayers中layer介绍

    1.base layers & overlay layers base layer:最底层的layer,其它的图层是在他之上,最先增加的图层默认作为base layer. overlay la ...

  3. C# - Garbage Collection

     The .NET Framework's garbage collector manages the allocation and release of memory for your appl ...

  4. 提高sqlite 的运行性能(转载)

    原文地址: https://blog.devart.com/increasing-sqlite-performance.html One the major issues a developer en ...

  5. GCJ Qualification Round 2016 C题

    题意是给定了一个叫“jamcoin”的定义,让你生成足够数量满足条件的jamcoin. jamcoin其实就可以理解成一个二进制整数,题目要求的要么长度为16位,要么为32位,一头一尾两个位必须是1, ...

  6. DuiLib笔记之设置文本字体

    设置文本字体要用到Font 它的常用属性如下 id 用于标识Font,类型:INT name 用于指定字体名称,类型:STRING size 用于指定字体大小,类型:INT bold 用于指定是否加粗 ...

  7. appium(7)-Automating mobile gestures

    While the Selenium WebDriver spec has support for certain kinds of mobile interaction, its parameter ...

  8. STM32 ~ ili9341 横屏驱动代码

    void ili9341_Initializtion(void) { u16 i; RCC->APB2ENR|=<<; //使能PORTB时钟 GPIOB->CRH&= ...

  9. 数组/字符串/ Math / 方法示例

    数组 Array concat  数组的合并 <script> var north = ["北京","上海","深圳"]; va ...

  10. javascript 正则表达式 详细入门教程

    1.什么是正则表达式 定义: 一个用来搜索.匹配.处理一些符合特定语法规则的一个强大的字符串处理工具. 用途: 进行特定字符和字符串的搜索 替换字符串中指定的字符或字符串 验证字符串是否符合需求 2. ...