format

作者: shuff1e | 来源:发表于2018-03-27 20:35 被阅读2次

https://www.cnblogs.com/wongbingming/p/6848701.html

print '位置参数 '

print '{0}---{1}'.format('abc',18)
print '{1},{0},{1}'.format('abc',18)                                                                                                          
print '{name}--->{age}'.format(age=18,name='abc')

print '类'

class Person(object):
    def __init__(self,name,age):
        self.name,self.age=name,age
    def __str__(self):
        return 'This guy is {self.name} , is {self.age} old.'.format(self=self)

print Person('shuffle',25)

print '对齐等'

print '{0:>8}'.format('189')
print '{0:0>8}'.format('189')

print '{0:a^8}'.format('189')
print '{0:a<8}'.format('189')

print '小数'

print '{0:.2f}'.format(321.3345)

print '二进制,八进制等'

print '{0:b}'.format(17)
print '{0:d}'.format(17)
print '{0:o}'.format(17)
print '{0:x}'.format(17)

print '格式转化'

# print '{0:,}'.format(1234567890)#NOT SUPPORTED IN PYTHON 2.6
# '{0!a}' convert argument to ascii not supported in python 2.6
p=Person('shuffle',25)
print '{0!s} is an instance of {1!r}'.format(p,Person,17)               
[root@shuffle-dev py_test]$ python sf_format.py 
位置参数 
abc---18
18,abc,18
abc--->18
类
This guy is shuffle , is 25 old.
对齐等
     189
00000189
aa189aaa
189aaaaa
小数
321.33
二进制,八进制等
10001
17
21
11
格式转化
This guy is shuffle , is 25 old. is an instance of <class '__main__.Person'>```

相关文章

网友评论

      本文标题:format

      本文链接:https://www.haomeiwen.com/subject/lwoscftx.html