Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/328.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 模数符号在这里做什么?_Python_Python 2.7_Modulo_Modulus - Fatal编程技术网

Python 模数符号在这里做什么?

Python 模数符号在这里做什么?,python,python-2.7,modulo,modulus,Python,Python 2.7,Modulo,Modulus,我理解模在规则中的用法,但它有多重用途吗? 例如,我看到: print "key is '%s'" %keydecrypt print "encrypted text is '%s'" %cipher 我不明白字符串中的模和变量旁边的模都在做什么。 -->它是将值替换为字符串的一种方法吗?它是一个文本占位符,在这种情况下,它将密钥解密和密码中的内容替换为字符串中的%s。因此,如果keydefcrypt和cipher是abc,则输出将是: key is 'abc' encrypted text

我理解模在规则中的用法,但它有多重用途吗? 例如,我看到:

print "key is '%s'" %keydecrypt
print "encrypted text is '%s'" %cipher
我不明白字符串中的模和变量旁边的模都在做什么。
-->它是将值替换为字符串的一种方法吗?

它是一个文本占位符,在这种情况下,它将
密钥解密
密码
中的内容替换为字符串中的
%s
。因此,如果
keydefcrypt
cipher
abc
,则输出将是:

key is 'abc'
encrypted text is 'abc'
但是,新函数更适合,因为它更明确:

print "key is '{0}'" .format(keydecrypt)
print "encrypted text is '{0}'".format(cipher)

是的。它在变量中进行替换。它还可以在格式方面做得更多。

此外,您不需要撇号

您还可以通过传递列表来使用多个变量。i、 e

print "key is: %s, encrypted text is: %s" % (keydecrypt, cipher)
除了%s,还有不同的类型,如%r

try:
    something
except Exception, e:
    print "The formatted Exception is: %r" % ex
一般来说,使用这种方法比使用另一种方法更好

print "This is my var: " + variable

请记住,在python中,一切都是对象。

它用于字符串格式设置。你可能想查一下,是的。它类似于
C
code中的
printf
样式格式。
%s
代码表示“对参数应用
str()
,并将结果插入到
%s
”的位置。