What is "\00" in Python?
Solution 1
In Python 2, when a number starts with a leading zero, it means it's in octal (base 8). In Python 3 octal literals start with 0o
instead. 00
specifically is 0.
The leading \
in \00
is a way of specifying a byte value, a number between 0-255. It's normally used to represent a character that isn't on your keyboard, or otherwise can't be easily represented in a string. Some special characters also have non-numeric "escape codes", like \n
for a newline.
The zero byte is also known as the nul byte or null byte. It doesn't display anything when printed -- it's null.
See http://www.ascii.cl/ for ASCII character codes.
Yes, replace
will still work with it, it just has no meaning as a display character.
It's sometimes used for other purposes, see http://en.wikipedia.org/wiki/Null_character.
Solution 2
The backslash followed by a number is used to represent the character with that octal value. So your \00
represents ASCII NUL.
Pankaj Upadhyay
Updated on July 09, 2022Comments
-
Pankaj Upadhyay almost 2 years
What does "\00" mean in Python? To learn more about this, I tried following:
- When I assign
d="\00"
and callprint d
, nothing displays on the screen. - I also tried assigning
d
to a string with extra spacing between and at the end and then calledd.replace("\00", "")
, but no result was evident.
What does
d.replace("\00","")
do? Will it merely look for this particular string "\00" and replace it with an empty string? - When I assign
-
Pankaj Upadhyay almost 13 yearsSo what will
replace
do ?. Will it merely treat "\00" as a plain string; to be replace by "". -
agf almost 13 yearsYes, it will replace the zero byte with whatever string you specify, just as if you replaced 'a' or ' ' (space) or '\t' (tab).
-
John Machin almost 13 yearsNo such thing as "ASCII NULL". It's
NUL
.NULL
is the SQL equivalent of Python'sNone
. -
Duncan almost 13 years@agf note that '\0' is actually base 8 not base 10. It doesn't make any difference for nulls, but as soon as you start using it for any character over 7 it will make a big difference.
-
agf almost 13 years@Duncan is right --
\0
is octal, while\x00
is hexadecimal, and both are null. -
mins over 2 years@JohnMachin, or NULL is to NUL what LINE FEED is to LF (meaning vs abbreviation)