字符串比较的一部分总是莫名其妙地返回False。

huangapple go评论54阅读模式
英文:

Part of string comparison always inexplicably returns False

问题

I have a simple Python script and I do not understand why I always get False as a return of that string comparison. What's wrong here?

inputStr = "upload test"
print(inputStr[:7] == "upload")  # -> False

In return the next code returns True:

inputStr = "upload"
print(inputStr[:7] == "upload")  # -> True

On the other hand when I use the keyword in, it works just fine (it prints out True):

inputStr = "upload test"
print("upload" in inputStr[:7])  # -> True

Can somebody explain this?

英文:

I have a simple Python script and I do not understand why I always get False as a return of that string comparison. What's wrong here?

inputStr = "upload test"
print(inputStr[:7] == "upload") # -> False

In return the next code returns True:

inputStr = "upload"
print(inputStr[:7] == "upload") # -> True

On the other hand when I use the keyword in, it works just fine (it prints out True):

inputStr = "upload test"
print("upload" in inputStr[:7]) # -> True

Can somebody explain this?

答案1

得分: 1

inputStr[:7] 返回的是 'upload ' 而不是 'upload'

此外,我建议您使用 str.startswith() 而不是切片!

示例:

print(inputStr.startswith("upload")) # -> True
英文:

inputStr[:7] returns 'upload ' and not 'upload'.

As well as that, I would suggest you use str.startswith() instead of slicing!

example:

print(inputStr.startswith("upload")) # -> True

答案2

得分: 1

以下是要翻译的内容:

这段代码 inputStr[:7] 在第一个块中的结果是:

> 'upload '

'upload ' 不等于 'upload'

这是因为它从索引 0 到 6 进行计数

你之所以在这个输入中得到 true:inputStr = "upload" 是因为字符串中只有 6 个字符

所以如果你想让第一个块返回 true
你应该使用这段代码:

inputStr = "upload test"
print(inputStr[:6] == "upload")
英文:

the results of this code inputStr[:7] in fist block is:

> 'upload '

and 'upload ' is not equal to 'upload'

because it counts from index 0 to 6

and the reason you get true in this input: inputStr = "upload"
is that there are only 6 characters in the string

so if you want fist block to return true
you should use this block of code:

inputStr = "upload test"
print(inputStr[:6] == "upload")

答案3

得分: 0

Case 1:

inputStr = "upload test"
print(inputStr[:7] == "upload")

它获取从0到6的字符(包括空格),这与“upload”不相等。所以将inputStr[:7]更改为inputStr[:6]

Case 2:
返回True,因为输入字符串中只有“upload”,所以inputStr[:7]仍然返回“upload”,与“upload”相等。

Case 3:
'in'关键字返回True,因为“upload”在“upload ”(inputStr[:7])中。

英文:

Case 1:

inputStr = "upload test"
print(inputStr[:7] == "upload")

it takes characters from 0 to 6 ("upload " with space) which is not equal to "upload". so change inputStr[:7] to inputStr[:6].

Case 2: returns True because in the input string there there is only "upload" so inputStr[:7] still gives "upload" which is equal to "upload".

Case 3: the 'in' keyword returns true because "upload" is in "upload "(inputStr[:7]).

答案4

得分: 0

Python索引从0开始。inputstr[::7] 返回"upload "(末尾有一个空格),而不是"upload"。
而在第二种情况下,inputstr[::7] 返回"upload",因为7超出了字符串的长度范围。

回答你的问题,语句应该是print(inputStr[:6] == "upload")

英文:

Python indexing is 0 based. inputstr[::7] returns "upload " (with a space at the end) not "upload".
whereas inputstr[::7] in the 2nd case returns "upload" because 7 is outside the length scope of the string.

To answer your question, the statement needs to be print(inputStr[:6] == "upload")

huangapple
  • 本文由 发表于 2023年5月14日 19:55:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/76247352.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定