相关疑难解决方法(0)

PHP有像Python的模板字符串这样的功能吗?

Python有一个称为模板字符串的功能.

>>> from string import Template
>>> s = Template('$who likes $what')
>>> s.substitute(who='tim', what='kung pao')
'tim likes kung pao'
Run Code Online (Sandbox Code Playgroud)

我知道PHP允许你写:

"Hello $person"
Run Code Online (Sandbox Code Playgroud)

并已$person替换,但模板可以在代码的各个部分重用?

php string templates template-variables

23
推荐指数
5
解决办法
2万
查看次数

如何在python中使用str.format()和字典?

这段代码有什么问题?

dic = { 'fruit': 'apple', 'place':'table' }
test = "I have one {fruit} on the {place}.".format(dic)
print(test)

>>> KeyError: 'fruit'
Run Code Online (Sandbox Code Playgroud)

python dictionary

17
推荐指数
2
解决办法
9304
查看次数

标签 统计

dictionary ×1

php ×1

python ×1

string ×1

template-variables ×1

templates ×1