Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
问题:
stra = """ this is a test line """ strb = " this is a test line"
stra 不等于 strb,因为在 stra 中引入了结束行字符。
请让我知道如何解决此问题?
使用方法strip删除尾随字符:
strip
>>> " x".strip() "x" >>> " x".strip() == "x ".strip() True
使用方法split逐字比较字符串:
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