zoukankan      html  css  js  c++  java
  • Python中字符串的Format用法。

    一、例子:

    "_".join(["1","2","3","4"])
    "_".join(map(lambda x:str(x),[1,2,3,4]))
    "{0}-{1}".format(3.4,34)
    "{}-{}-{}".format(3,4,5)
    "{name}-{age}".format(**{'name':'song','age':34})
    "{name}-{age}".format(name='song',age=34)
    "the list first is: {obj[0]}".format(obj=[1,2,3,4])
    "the list first is: {0[0]},{0[1]}".format([1,2,3,4])
    "{:>8}".format('abc')
    "{0:<8}".format('abc')
    "{0:_<8}".format('abc')
    "{0:0>8}".format('3.14')
    "{0:.2f}".format(123.123456)
    "{0:b}".format(1023)
    "{0:d}".format(0b1111111111)
    "{0:x}".format(1023)
    "{0:,}".format(102345.6789)

    二、结果:

    >>> "_".join(["1","2","3","4"])
    '1_2_3_4'
    >>> "_".join(map(lambda x:str(x),[1,2,3,4]))
    '1_2_3_4'
    >>> "{0}-{1}".format(3.4,34)
    '3.4-34'
    >>> "{}-{}-{}".format(3,4,5)
    '3-4-5'
    >>> "{name}-{age}".format(**{'name':'song','age':34})
    'song-34'
    >>> "{name}-{age}".format(name='song',age=34)
    'song-34'
    >>> "the list first is: {obj[0]}".format(obj=[1,2,3,4])
    'the list first is: 1'
    >>> "the list first is: {0[0]},{0[1]}".format([1,2,3,4])
    'the list first is: 1,2'
    >>> "{:>8}".format('abc')
    '     abc'
    >>> "{0:<8}".format('abc')
    'abc     '
    >>> "{0:_<8}".format('abc')
    'abc_____'
    >>> "{0:0>8}".format('3.14')
    '00003.14'
    >>> "{0:.2f}".format(123.123456)
    '123.12'
    >>> "{0:b}".format(1023)
    '1111111111'
    >>> "{0:d}".format(0b1111111111)
    '1023'
    >>> "{0:x}".format(1023)
    '3ff'
    >>> "{0:,}".format(102345.6789)
    '102,345.6789'
    >>> 
  • 相关阅读:
    Model
    暑假集训-计算几何
    暑假集训-字符串
    将博客搬至CSDN
    codeforces #519 A A. Multiplication Table (暴力)
    bc #54 A problem of sorting
    vimrc备份
    codeforces # 317 div 2 B. Order Book(模拟)
    codeforces #317 div2 A. Arrays (水)
    bc #52 div 2 A ||hdoj5417 Victor and Machine (模拟)
  • 原文地址:https://www.cnblogs.com/songxingzhu/p/7056847.html
Copyright © 2011-2022 走看看