Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is "\00" in Python?

What does "\00" mean in Python? To learn more about this, I tried following:

  • When I assign d="\00" and call print d, nothing displays on the screen.
  • I also tried assigning d to a string with extra spacing between and at the end and then called d.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?

like image 229
Pankaj Upadhyay Avatar asked Jul 27 '11 07:07

Pankaj Upadhyay


People also ask

What is the null char in Python?

'\0' is defined to be a null character. It is a character with all bits set to zero.

What is CHR 00?

chr(0) is NULL character, which is very significant and chr(32) is ' ' . The point of NULL character is to terminate strings for example. So what you see like x = "abcd" is actaully x = "abcd\00" , where of course \00 is the same as chr(0) .

What is x00 in Ascii?

\x is used to denote an hexadecimal byte. \x00 is thus a byte with all its bits at 0. (As Ryne pointed out, a null character translates to this.) Other examples: \xff is 11111111, \x7f is 01111111, \x80 is 10000000, \x2c is 00101010, etc. 7th September 2016, 1:40 AM.

How do I remove a null character from a string in Python?

The str. replace() method will remove occurrences of the \x00 character by replacing them with an empty string. Copied! The \x00 character is a Null-character that represents a HEX byte with all bits at 0.


2 Answers

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.

like image 122
agf Avatar answered Oct 03 '22 15:10

agf


The backslash followed by a number is used to represent the character with that octal value. So your \00 represents ASCII NUL.

like image 37
Jacob Avatar answered Oct 03 '22 16:10

Jacob