全国旗舰校区

不同学习城市 同样授课品质

北京

深圳

上海

广州

郑州

大连

武汉

成都

西安

杭州

青岛

重庆

长沙

哈尔滨

南京

太原

沈阳

合肥

贵阳

济南

下一个校区
就在你家门口
+
当前位置:首页  >  技术干货

python 字符串格式化

发布时间:2023-11-08 00:22:00
发布人:xqq

1."旧式字符串解析(%操作符)"

'Hello,%s'%name

"Hello,Bob"

'Hey%(name)s,thereisa0x%(errno)xerror!'%{

"name":name,"errno":errno}

'HeyBob,thereisa0xbadc0ffeeerror!'

2."新式"字符串格式化(str.format)

'Hello,{}'.format(name)

'Hello,Bob'

'Hey{name},thereisa0x{errno:x}error!'.format(name=name,errno=errno)

'HeyBob,thereisa0xbadc0ffeeerror!'

3.字符串插值/f-Strings(Python3.6+)

python3.6新出的,本人用了这个之后,果断抛弃其他方法,真的太强大了!!!

name=Bob

f'Hello,{name}!'

'Hello,Bob!'

defgreet(name,question):

...returnf"Hello,{name}!How'sit{question}?"

...

greet('Bob','going')

"Hello,Bob!How'sitgoing?"

4.字符串模板法(Python标准库)

templ_string='Hey$name,thereisa$errorerror!'

Template(templ_string).substitute(

...name=name,error=hex(errno))

'HeyBob,thereisa0xbadc0ffeeerror!'

以上内容为大家介绍了python字符串格式化,希望对大家有所帮助,如果想要了解更多Python相关知识,请关注IT培训机构:千锋教育。

python培训

相关文章

Python 命名空间的生命周期

Python 命名空间的生命周期

2023-11-08
Python 命名空间的访问

Python 命名空间的访问

2023-11-08
python 编译器

python 编译器

2023-11-08
python pyc 文件

python pyc 文件

2023-11-08

最新文章

武汉新媒体行业公司排名

武汉新媒体行业公司排名

2023-11-01
武汉新媒体就业现状好吗

武汉新媒体就业现状好吗

2023-11-01
武汉全媒体行业发展现状及趋势

武汉全媒体行业发展现状及趋势

2023-10-31
武汉全媒体现状

武汉全媒体现状

2023-10-31
在线咨询 免费试学 教程领取