字符串格式化方法str.format()功能十分强大,在平时应用也比较多,今天我们来了解一下str.format()方法
>>> 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 '}').
帮助文档显示的内容非常简单。下面介绍具体应用常见的几种格式化方式:
'Hello {}'.format('Word')===>Hello Word
-------------------------------------------------
'你喜欢{2}吗?难道你喜欢{1},我猜你喜欢{0}'.format('java','c++','python')
===>你喜欢python吗?难道你喜欢c++,我猜你喜欢java
---------------------------------------------------
name='james'
age=12
'我的名字是{},年龄是{}'.format(name,age)
===>我的名字是james,年龄是12
------------------------------------------

本文详细介绍了Python编程中字符串格式化方法str.format()的使用,包括多种常见格式化方式。通过实例解析,帮助理解其功能和应用。
最低0.47元/天 解锁文章
12万+

被折叠的 条评论
为什么被折叠?



