转自 https://www.cnblogs.com/gide/p/6955895.html

python2.6开始,新增了一种格式化字符串的函数str.format(),此函数可以快速处理各种字符串。

它通过{}和:来代替%。

#通过位置
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

日期格式也可以使用format

python format()用法的更多相关文章

  1. python format 用法详解

    format 用法详解 不需要理会数据类型的问题,在%方法中%s只能替代字符串类型 单个参数可以多次输出,参数顺序可以不相同 填充方式十分灵活,对齐方式十分强大 官方推荐用的方式,%方式将会在后面的版 ...

  2. 关于python format()用法详解

    str.format() 这个特性从python2.6而来 其实实现的效果和%有些类似 不过有些地方更方便 通过位置映射: In [1]: '{0},{1}'.format('kzc',18) Out ...

  3. python format用法详解

    #常用方法:print('{0},{1}'.format('zhangk', 32)) print('{},{},{}'.format('zhangk','boy',32)) print('{name ...

  4. python基础_格式化输出(%用法和format用法)

      目录 %用法 format用法 %用法 1.整数的输出 %o —— oct 八进制%d —— dec 十进制%x —— hex 十六进制 1 >>> print('%o' % 2 ...

  5. python format()函数的用法

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

  6. python基础_格式化输出(%用法和format用法)(转载)

    python基础_格式化输出(%用法和format用法) 目录 %用法 format用法 %用法 1.整数的输出 %o -- oct 八进制%d -- dec 十进制%x -- hex 十六进制 &g ...

  7. 【Python笔记】1、格式化输出(%用法和format用法)

    转自:https://www.cnblogs.com/fat39/p/7159881.html 一.格式化输出1.整数的输出%o —— oct 八进制%d —— dec 十进制%x —— hex 十六 ...

  8. python format格式化函数用法

    python format格式化函数用法 原文 Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能. 基本语法是通过 {} 和 : 来代替以前 ...

  9. python格式化输出之format用法

    format用法 相对基本格式化输出采用‘%’的方法,format()功能更强大,该函数把字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号‘{}’作为特殊字符代替‘%’ 使用方法由两种:b ...

随机推荐

  1. 网易im即时通讯 移动端嵌入web

    近期产品需求要在wapapp 内部嵌入网易im聊天客服功能,内部需求不是很多,不过还是第一次接触,有点抓耳,,, 希望召集更多大神交流878269930 增加用户默认发送自定义消息功能:

  2. react_app 项目开发 (5)_前后端分离_后台管理系统_开始

    项目描述 技术选型 react API 接口 接口文档,url,请求方式,参数类型, 根据文档描述的方法,进行 postman 测试,看是否能够得到理想的结果 collections - 创建文件取项 ...

  3. [LeetCode] Card Flipping Game 翻卡片游戏

    On a table are N cards, with a positive integer printed on the front and back of each card (possibly ...

  4. Eclipse导出自己的项目(仅供自己保留方式war包)

    War: Jar包:

  5. 2019.3.23 python的unittest框架与requests

    (明天学测试用例集合及输出测试报告!!!) import unittest import requests import json class Test_get(unittest.TestCase): ...

  6. CSS3属性上调

    一.为什么使用CSS 1.有效的传递页面信息 2.使用CSS美化过的页面文本,使页面漂亮.美观,吸引用户 3.可以很好的突出页面的主题内容,使用户第一眼可以看到页面主要内容 4.具有良好的用户体验 二 ...

  7. verilog 之流水灯

    1.黑金板 简易操作: 通过判断数值累加    个人观点:黑金代码质量有待提高,讲解不够详细 2.正点原子的 位置调换 led[:] <= {led[:],led[]}; 3.传统位移 led& ...

  8. Linux和Shell教程

    文档资料参考: 参考:http://www.runoob.com/linux/linux-tutorial.html 软件下载参考: centos 下载地址:https://www.centos.or ...

  9. Python extend()方法--list

    描述 extend()方法:在列表末尾追加可迭代对象中的元素. 语法 语法格式:list.extend(iterable) 参数 iterable:可迭代的对象,这里的对象可以是字符串.列表.元组.字 ...

  10. DAX/PowerBI系列 - 玩转阿里云 Alicloud Pricing

    DAX/PowerBI系列 - 玩转 阿里云主机 Ali Cloud ECS 难度: ★★☆☆☆(1星) 适用范围: ★★★☆☆(3星) 欢迎交流与骚扰 这是啥: 双十一就到了,码农门,程序猿们有没有 ...