1

问题:

stra = """ this
is
a
test 
line """

strb = " this is a test line"

stra 不等于 strb,因为在 stra 中引入了结束行字符。

请让我知道如何解决此问题?

4

1 回答 1

3

使用方法strip删除尾随字符:

>>> " x".strip()
"x"
>>> " x".strip() == "x ".strip()
True

使用方法split逐字比较字符串:

>>> " x y\nz".split() # return list of "words" separated by whitespace characters
["x", "y", "z"]
>>> " x y\nz".split() == "x  y z".split()
True
于 2012-10-10T21:30:32.073 回答