python格式化函数foramt,,#通过位置print
python格式化函数foramt,,#通过位置print
#通过位置
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
本文章转自https://www.cnblogs.com/gide/p/6955895.html
python格式化函数foramt
相关内容
- python 命名元组(namedtuple),,我们知道c/c++语
- python GM11,,常见系统分类:白色系
- Python 案例一(计算人体体脂率),,#计算人体体脂率
- 用python实现小时候的石子游戏,,# 俩子抵一个# 游
- Linux wget命令下载各版本的Python方法,, python3.
- Python函数参数详解,,Python函数参数
- python瀹炰緥_宸ヨ祫绠$悊绯荤粺,,鏍囩锛?a hre
- Python程序设计实验二,,Python程序设计
- python程序设计基础(程序设计基础方法),,python初学者程
- Python3---常见函数---type(),,0X01;功能描述
评论关闭