默认替换 python 脚本中的 %s
有时在Python脚本中我会看到这样的行:
cmd = "%s/%s_tb -cm cond+line+fsm -ucli -do \"%s\""
上面行中的 %s
被替换在哪里? Python 是否有一些字符串堆栈,它会弹出它们并替换 %s
吗?
Sometimes in Python scripts I see lines like:
cmd = "%s/%s_tb -cm cond+line+fsm -ucli -do \"%s\""
Where is the %s
in the above line substituted? Does Python have some stack of strings and it pops them and replaces %s
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
Python字符串格式化的基础知识
不是你的代码行的具体答案,但既然你说你是Python新手,我想我会用这个作为例子来分享一些快乐;)
简单的例子内联列表:
使用字典的简单示例:
如有疑问,请查看 python 官方文档 - http://docs.python.org/library/stdtypes.html#string-formatting
Basics of python string formatting
Not a specific answer to your line of code, but since you said you're new to python I thought I'd use this as an example to share some joy ;)
Simple Example Inline With a List:
Simple Example Using a Dictionary:
When in doubt, check the python official docs - http://docs.python.org/library/stdtypes.html#string-formatting
稍后将在类似这样的情况下使用:
您所看到的只是一个字符串赋值,其中包含稍后将被填充的字段。
That would be later used in something like:
What you're seeing is just a string assignment with fields in it which will later be filled in.
它用于字符串插值。
%s
被替换为字符串。您可以使用模运算符 (%
) 进行字符串插值。该字符串将位于左侧,替换各种%s
的值位于右侧,位于元组中。如果您只有一个字符,您可能会忘记元组。
在较新版本的 python 中,推荐的方法是使用字符串类型的
format
方法:It's being used for string interpolation. The
%s
is replaced by a string. You use the modulo operator (%
) to do string interpolation. The string will be on the left side, the values to substitute for the various%s
are on the right, in a tuple.If you have just a single character you can forget the tuple.
In newer versions of python the recommend way is to use the
format
method of the string type: