为什么python字符串中的“%”会使传输字符无效

时间:2018-09-29 06:52:54

标签: python python-3.x

t = ("rotation: %d%%\t [note]"% 123)
in jupyter notebook when you type t and run cell
output: rotation: 123%\t [note]

我想得到的结果如下:

输出:旋转:123%[note]

2 个答案:

答案 0 :(得分:1)

这是jupyter笔记本的体现。使用str默认使用的print时,我们得到

rotation: 123%   [note]

但是,当您在交互式python中键入t时,将使用repr()来代替。可以使用任何交互式python轻松复制它:

>>> t = ("rotation: %d%%\t [note]"% 123)
>>> t
'rotation: 123%\t [note]'
>>> print(t)
rotation: 123%   [note]

区别在于repr()给出了对象的表示形式,以便可以通过代码重新创建它,并且对于调试最有用。 str()(和print)给出了人类(最终用户)可读的形式。

另请参阅:str() vs repr() functions in python 2.7.5也适用于python 3。

答案 1 :(得分:0)

import sys
sys.stdout.write("rotation: %d%%\t [note]"% 123)

这应该有效