Python3 中的str.format方法

39 篇文章 23 订阅 ¥39.90 ¥99.00
本文探讨Python3中str.format方法,该方法用于格式化字符串,通过大括号'{}'作为替代物。虽然说明中有两个参数args和kwargs,但实际使用中,它们代表替换选择,可以单独或同时使用。若同时使用,未定义关键字参数会出现在定义关键字参数之前。在调用format方法时,传入的参数数量需符合字符串中替代物的数量要求,否则将引发错误。
摘要由CSDN通过智能技术生成

在python中使用help命令窗口该方法获得如下结果

>>> help(str.format)
Help on method_descriptor:

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 '}').

根据上述的方法解释,是指该方法是用来返回字符串的格式化后结果,在该方法中在参数args与kwargs使用了替代物,替代物需要用大括号'{}'标识。

按照上述说法,该方法有两个参数,可是查找了半天,也没有见到有两个参数的使用。后来在帮助文档中发现如下方法说明

str.format(*args, **kwargs)
Perform a string formatting operation. The string on which this method is called can 
contain literal text or replacement fields delimited by braces {}. Each replacement field
 contains either the numeric index of a positional argument, or the name of a keyword argument.
 Returns a copy of the string where each replacement field 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

mystonelxj

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值