python format

//test.py

print "{} {}".format("hello", "world")

print '{0} {1}'.format('hello', 'world')

print '{1} {0} {1}'.format('hello', 'world')

print 'website: {name}, addr: {url}'.format(name='zcl', url='www.zcl.com')

site = {'name':'zcl', 'url':'www.zcl.com', 'other':'guess'}

print 'website: {name}, addr: {url}, other: {other}'.format(**site)

_list = ['zcl', 'www.zcl.com']

print 'website: {0[0]}, addr: {0[1]}'.format(_list)

print ('{:.2f}'.format(3.1415926))

print ('{:+.2f}'.format(3.1415926))

print ('{:b}'.format(11))

class AssignValue(object):

        def __init__(self, value):

                self.value = value

my_value = AssignValue(6)

print 'value is :{0.value}'.format(my_value)

print 'end'

//result

# python test.py
hello world
hello world
world hello world
website: zcl, addr: www.zcl.com
website: zcl, addr: www.zcl.com, other: guess
website: zcl, addr: www.zcl.com
3.14
+3.14
1011
value is :6
end

原文地址:https://www.cnblogs.com/woodzcl/p/7833818.html