字符串format

    def format(self, *args, **kwargs): # known special case of str.format
        """
        S.format(*args, **kwargs) -> str
        
        Return a formatted version of S, using substitutions from args and kwargs.
        The substitutions are identified by braces ('{' and '}').
        """
        pass

最关键的the substitutions 将被替换的东西是用{  }括起来的。。。。

三种玩法:

res='{name}{age}{sex}'.format(age=18,name='wesley',sex='male')
print(res)

res='{}{}{}'.format('wesley','hello',"name")
print(res)

res='{1}{0}{1}'.format('wesley',18)    #虽然有三个{},但因为只需2个值填充,所以不报错,当然写三个也不报错
print(res)

wesley18male
wesleyhelloname
18wesley18

原文地址:https://www.cnblogs.com/wuxi9864/p/9868727.html