可以在第一次创建变量时将模数 (%s) 放在字符串中,然后再使用吗?
Can modulo (%s) be placed in string when first creating a variable and then used later on?
我是 python 的新手,如果这是一个愚蠢的问题,请原谅我。我知道如何以这种方式在字符串中使用模数
Me = "I'm %s and I like to %s" % ('Mike', 'code')
但是,通过我的搜索,我还没有找到是否可以将模数硬编码到字符串中,然后再利用它的答案。
示例:
REPO_MENU = {'Issues':Api.github/repo/%s/branch/%s/issues,
'Pull Requests':'Api.github/repo/%s/branch/%s/pull_requests',
'Commits':'Api.github/repo/%s/branch/%s/commits'
'<FILTER>: Branch':'Api.github/repo/%s/branch/%s'
}
for key, value in REPO_MENU.items():
Print value % ('Beta', 'master')
这种格式行得通吗?使用这种方法是好的做法吗?我觉得它在很多情况下都是有益的。
这确实有效。您还可以使用 format 函数,效果很好。例如:
menu1 = {'start':'hello_{0}_{1}',
'end':'goodbye_{0}_{1}'}
menu2 = {'start':'hello_%s_%s',
'end':'goodbye_%s_%s'}
for key, value in menu1.items():
print value.format('john','smith')
for key, value in menu2.items():
print value %('john','smith')
%
和其他运算符一样;当它的左手操作数是一个字符串时,它会尝试用右手操作数中的值替换各种占位符。左边的操作数是字符串文字还是更复杂的表达式并不重要,只要它的计算结果是字符串即可。
正如其他答案所指出的,您绝对可以对同一个字符串多次执行字符串模运算。但是,如果您正在使用 Python 3.6(如果可以,您绝对应该!),我建议您 use fstrings rather than the string-modulo or .format
. They are faster,更容易阅读,也很方便:
A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.
所以 f 字符串也是可移植的,就像其他格式化选项一样。
例如:
>>> value = f'A {flower.lower()} by any name would smell as sweet.'
>>> flower = 'ROSE'
>>> print(value)
A rose by any name would smell as sweet.
>>> flower = 'Petunia'
>>> print(value)
A petunia by any name would smell as sweet.
>>> flower = 'Ferrari'
>>> print(value)
A ferrari by any name would smell as sweet.
您可以使用 f 字符串将其添加到任何模块的顶部,作为对其他用户(或未来的您)的有用提醒:
try:
eval(f'')
except SyntaxError:
print('Python 3.6+ required.')`.
raise
我是 python 的新手,如果这是一个愚蠢的问题,请原谅我。我知道如何以这种方式在字符串中使用模数
Me = "I'm %s and I like to %s" % ('Mike', 'code')
但是,通过我的搜索,我还没有找到是否可以将模数硬编码到字符串中,然后再利用它的答案。
示例:
REPO_MENU = {'Issues':Api.github/repo/%s/branch/%s/issues,
'Pull Requests':'Api.github/repo/%s/branch/%s/pull_requests',
'Commits':'Api.github/repo/%s/branch/%s/commits'
'<FILTER>: Branch':'Api.github/repo/%s/branch/%s'
}
for key, value in REPO_MENU.items():
Print value % ('Beta', 'master')
这种格式行得通吗?使用这种方法是好的做法吗?我觉得它在很多情况下都是有益的。
这确实有效。您还可以使用 format 函数,效果很好。例如:
menu1 = {'start':'hello_{0}_{1}',
'end':'goodbye_{0}_{1}'}
menu2 = {'start':'hello_%s_%s',
'end':'goodbye_%s_%s'}
for key, value in menu1.items():
print value.format('john','smith')
for key, value in menu2.items():
print value %('john','smith')
%
和其他运算符一样;当它的左手操作数是一个字符串时,它会尝试用右手操作数中的值替换各种占位符。左边的操作数是字符串文字还是更复杂的表达式并不重要,只要它的计算结果是字符串即可。
正如其他答案所指出的,您绝对可以对同一个字符串多次执行字符串模运算。但是,如果您正在使用 Python 3.6(如果可以,您绝对应该!),我建议您 use fstrings rather than the string-modulo or .format
. They are faster,更容易阅读,也很方便:
A formatted string literal or f-string is a string literal that is prefixed with 'f' or 'F'. These strings may contain replacement fields, which are expressions delimited by curly braces {}. While other string literals always have a constant value, formatted strings are really expressions evaluated at run time.
所以 f 字符串也是可移植的,就像其他格式化选项一样。
例如:
>>> value = f'A {flower.lower()} by any name would smell as sweet.'
>>> flower = 'ROSE'
>>> print(value)
A rose by any name would smell as sweet.
>>> flower = 'Petunia'
>>> print(value)
A petunia by any name would smell as sweet.
>>> flower = 'Ferrari'
>>> print(value)
A ferrari by any name would smell as sweet.
您可以使用 f 字符串将其添加到任何模块的顶部,作为对其他用户(或未来的您)的有用提醒:
try:
eval(f'')
except SyntaxError:
print('Python 3.6+ required.')`.
raise