Python 字符串中出现意外的空字符串 [英] Unexpected empty strings within Python strings

查看:91
本文介绍了Python 字符串中出现意外的空字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

观察以下互动会话:

In [1]: s = 'emptiness'

In [2]: s.replace('', '*')
Out[2]: '*e*m*p*t*i*n*e*s*s*'

In [3]: s.count('')
Out[3]: 10

我今天发现了这个,对我来说有点困惑和惊讶.

I discovered this today, and it is a little confusing and surprising for me.

我喜欢学习有关 Python 的此类知识,但似乎这可能会导致一些非常令人困惑的问题.例如,如果空字符串作为变量传入,而只是碰巧是一个空字符串,那么您最终可能会得到一些令人惊讶的结果.该行为似乎也有点不一致,因为基于上面的交互式会话,我认为以下将生成字符串中所有字符的列表(类似于 JavaScript 行为).相反,您会收到一个错误:

I love learning things like this about Python, but it seems like this could lead to some pretty confusing gotchas. For example, if the empty string was passed in as a variable, and just happened to be an empty string, you could end up with some surprising consequences. The behavior also seems a little inconsistent, because based on the interactive session above, I would think that the following would produce a list of all the characters in the string (similar to the JavaScript behavior). Instead, you get an error:

In [4]: s.split('')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-4-c31bd2432bc1> in <module>()
----> 1 s.split('')

ValueError: empty separator

此外,这会导致 str.endswith()str.startswith() 的一些看似矛盾的行为:

Also, this leads to some seemingly contradictory behavior with str.endswith() and str.startswith():

In [5]: s.endswith('')
Out[5]: True

In [6]: s.endswith('s')
Out[6]: True

In [7]: s.startswith('')
Out[7]: True

In [8]: s.startswith('e')
Out[8]: True

尝试各种字符串方法,你可以找到更多同样奇怪的例子.

Experimenting with various string methods, you can find more similarly strange examples.

我的问题是为什么空字符串会这样?或者这是 str 方法如何处理空字符串的结果?如果有人有任何见解,或者可以指出我对这种行为的解释/描述的方向,那就太棒了.

My question is why does the empty string behave this way? Or is this the result of how the str methods are handling empty strings? If anyone has any insights, or can point me in the direction of an explanation/description of this behavior, that would be awesome.

推荐答案

Python 字符串遵循空字符串是所有其他字符串的子集的原则.此外,python 字符串也是字节字符串的串联,这意味着字符串由夹在空字符串之间的字节组成.您可以通过以下示例看到这一点:

Python strings follow the principle that an empty string is a subset of every other string. Furthermore, python strings are also concatenations of byte strings, implying that a string consists of bytes sandwiched between empty strings. You can see that by the following examples:

>>>'a'.count('')
2
>>>'aa'.count('')
3
>>>'string'.count('')
7

所以'a'必须是''+'a'+'',而'aa'必须是''+'a'+''+'a'+''.

So 'a' must be ''+'a'+'', and 'aa' must be ''+'a'+''+'a'+''.

当您检查 'a'.startswith('') 时,它会看到字符串 'a' 技术上以空字符串开头.'a'.endswith('') 也一样.但是,当您检查 'a'.startswith('a') 时,它会忽略空字符串并查看第一个字节.

When you check 'a'.startswith(''), it sees that the string 'a' technically starts with an empty string. Same for 'a'.endswith(''). However when you check 'a'.startswith('a'), it ignores the empty string and looks at the first byte.

这篇关于Python 字符串中出现意外的空字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆