Do Python strings end in a terminating NULL?

There is no end-of-string character in Python, at least not one that is exposed and it would be implementation dependent. String objects maintain their own length and it is not something that you need to be concerned with. There are several ways to get the string's length without using len().

str = 'man bites dog'
unistr = u'abcd\u3030\u3333'

# count characters in a loop
count = 0
for character in str:
    count += 1
>>> count
13

# works for unicode strings too
count = 0
for character in unistr:
    count += 1
>>> count
6

# use `string.count()`
>>> str.count('') - 1
13
>>> unistr.count(u'') - 1
6

# weird ways work too
>>> max(enumerate(str, 1))[0]
13
>>> max(enumerate(unistr, 1))[0]
6
>>> str.rindex(str[-1]) + 1
13
>>> unistr.rindex(unistr[-1]) + 1
6

# even weirder ways to do it
import re
pattern = re.compile(r'$')
match = pattern.search(str)
>>> match.endpos
13
match = pattern.search(unistr)
>>> match.endpos
6

I suspect that this is just the tip of the iceberg.


Several interesting things I found:

s_1 = '\x00'
print ("s_1 : " + s_1)
print ("length of s_1:  " + str(len(s_1)))

s_2 = ''
print ("s_2 : " + s_2)
print ("length of s_2:  " + str(len(s_2)))

if s_2 in s_1:
    print ("s_2 in s_1")
else:
    print ("s_2 not in s_1")

The output is:

s_1 :  
length of s_1:  1
s_2 : 
length of s_2:  0
s_2 in s_1

Here s_1 seems like a ' ' and s_2 seems like a '' or a NULL.


To answer the question that you asked: there isn't a terminating NULL or anything like that on the end of a Python string (that you can see), because there's no way for you to "fall off the end" of a string. Internally, the most popular Python implementation is written in C, so there probably is a NULL-terminated string somewhere under the hood. But that's completely opaque to you as a Python developer.

If you want to get the length without using a builtin function, you can do a number of different things. Here's an option that's different from the others posted here:

sum([1 for _ in "your string goes here"])

which is, in my opinion, a little more elegant.

Tags:

Python