之间有什么区别__str__和__repr__在__str__?
当我创建一个包含反斜杠的字符串时,它们会重复:
>>> my_string = "why\does\it\happen?"
>>> my_string
'why\\does\\it\\happen?'
Run Code Online (Sandbox Code Playgroud)
为什么?
我正在尝试在列表中使用 f 字符串。我创建了一个新的行变量。标准变量工作正常,但新行变量不行
NL = '\n'
var = 'xyz'
lst = []
print(f'test{NL}new line')
lst.append(f"first line var is {var}{NL}a second line {NL}")
lst.append(f"third line{NL}forth line var is {var}")
print(lst)
Run Code Online (Sandbox Code Playgroud)
创建输出
test
new line
['first line var is xyz\na second line \n', 'third line\nforth line var is xyz']
Run Code Online (Sandbox Code Playgroud)
我怎样才能做到这一点?