Python 3.x 当我打印'chr(13)`来自我的类的变量'\r";取而代之的是印刷品

Python 3.x 当我打印'chr(13)`来自我的类的变量'\r";取而代之的是印刷品,python-3.x,Python 3.x,当我转到“print(ic.allopt)”时,我的allopt段中的chr(13)将打印\n。当我从类中调用该变量时,我试图打印一行新行。谢谢 class Dialogue: def __init__(self, seed, prompt, opt1, opt2, opt3, opt4): self.id = seed self.prompt = prompt self.opt1 = opt1 self.opt2 = op

当我转到“
print(ic.allopt)
”时,我的
allopt
段中的
chr(13)
将打印
\n
。当我从类中调用该变量时,我试图打印一行新行。谢谢

class Dialogue:
    def __init__(self, seed, prompt, opt1, opt2, opt3, opt4):
        self.id = seed
        self.prompt = prompt
        self.opt1 = opt1
        self.opt2 = opt2
        self.opt3 = opt3
        self.opt4 = opt4
        self.allopt = prompt, chr(13), "A)", opt1, "B)", opt2, "C)", opt3, "D)", opt4


openingscene = "hello weary traveller"
ic = Dialogue(1, "We came in and saw you on the ground, you looked pretty out of it, we tidied you up and tucked you "
                 "in. So. You have a name?", "My name is:", "Why do you want to know my name", "3", "4")


def initialclass():
    print(openingscene)
    print(ic.allopt)

initialclass()```


所发生的事情是,print向您显示
allopt
(一个元组)中的项目,没有任何格式。第二,回车,
chr(13)
,不是换行符,而是Windows中换行符的一部分。在Windows中,默认换行符都是chars
\r\n
,但我将使用
\n
作为示例

最简单的解决方案是告诉
print
将元组视为一系列参数,而不是单个元组参数

In [1]: x = 5, chr(10), 6

In [2]: print(x)
(5, '\n', 6)

In [3]: print(*x)
5
 6
您可以使用
*变量
符号来实现这一点。请注意,ASCII字符10是
\n

否则,您应该以您认为合适的方式格式化字符串。也许可以将该逻辑包装为一个方法,返回一个准备打印的字符串


祝你好运

要打印新行,应使用
'\n'
chr(10)

第二个问题是,
self.allopt
是一个元组。为了打印元组的内容,应该使用
print(*ic.allopt)
进行扩展,或者使用
print(“.join(ic.allopt))
创建字符串


总的来说,我建议你继续读下去。我发现这对您来说是一个更好的解决方案。

对您的问题的回答也是对以下问题的正确回答:

如何在Python中连接字符串?

我把你的例子简单化了:

def foo(self, seed, prompt, opt1, opt2, opt3, opt4):
    x = prompt, chr(13), "A)", opt1, "B)", opt2, "C)", opt3, "D)", opt4
    return x

r = foo(1, "hello mars", "pluto", "strawbery", "axe", "3", "4")

print(r)
输出为:

('pluto', '\r', 'A)', 'strawbery', 'B)', 'axe', 'C)', '3', 'D)', '4')
这是因为
提示符,chr(13),[…]
返回一个元组,而不是字符串。 您没有将小字符串合并为一个大字符串。
相反,您创建了一个索引容器(一个元组),这样元组的每个元素都是一个字符串

请尝试以下操作:

import io

class Dialogue:
    def __init__(self, seed, prompt, opt1, opt2, opt3, opt4):
        self.id = seed
        self.prompt = prompt
        self.opt1 = opt1
        self.opt2 = opt2
        self.opt3 = opt3
        self.opt4 = opt4
        str_strm = io.StringIO()
        print(
            prompt,
            chr(13),
            "A)", opt1,
            "B)", opt2,
            "C)", opt3,
            "D)", opt4, sep=" ", end="\n", file=str_strm)
        self.allopt = str_strm.getvalue()

obj = Dialogue('strawberry', 'xxx', 'axe', 'yyyy', 'fgdff', 'fdgdfg')

print(obj.allopt)
您不需要手动插入
chr(13)
“\n”

Python的
print
函数将在末尾为您添加一行新行:

        str_strm = io.StringIO()
        print(prompt, file=str_strm)
        print("A)", opt1, file=str_strm)
        print("B)", opt2, file=str_strm)
        print("C)", opt3, file=str_strm)
        print("D)", opt4, file=str_strm)
        self.allopt = str_strm.getvalue()
连接字符串的另一种方法如下所示:

merged_string = "".join(['str1', 'str2', 'str3', 'str4', 'fgdff', 'fdgdfg'])