Hi all,
Forgive my pool English first...
I'm learning Python recently. But I met with a problem yesterday and I have liitle experience in solving this kind of problems.
Here are the code in Python Shell:

>>> "HuXiaoxiang\\0Nanjing University\03 Years.".replace("\0"," ")
'HuXiaoxiang\\0Nanjing University\x03 Years.'
>>> "HuXiaoxiang \0Nanjing University\03 years.".replace("\0"," ")
'HuXiaoxiang  Nanjing University\x03 years.'
>>> "HuXiaoxiang \0Nanjing University\03 years.".replace(" ","@")
'HuXiaoxiang@\x00Nanjing@University\x03@years.'

Why the first & second didn't work as I excepted?
Thxs for anyone who will view my problem in advance~

Hi all,
Forgive my pool English first...
I'm learning Python recently. But I met with a problem yesterday and I have liitle experience in solving this kind of problems.
Here are the code in Python Shell:

>>> "HuXiaoxiang\\0Nanjing University\03 Years.".replace("\0"," ")
'HuXiaoxiang\\0Nanjing University\x03 Years.'
>>> "HuXiaoxiang \0Nanjing University\03 years.".replace("\0"," ")
'HuXiaoxiang  Nanjing University\x03 years.'
>>> "HuXiaoxiang \0Nanjing University\03 years.".replace(" ","@")
'HuXiaoxiang@\x00Nanjing@University\x03@years.'

Why the first & second didn't work as I excepted?
Thxs for anyone who will view my problem in advance~

Your problem is not with the replace() method but with the escaped characters in literal strings:

  • An escaped backslash "\\" is interpreted as a single backslash character in a literal string, so your first string contains a backslash character followed by the digit 0, and not the null character '\0'.
  • A backslash followed by 1, 2 or 3 octal digits is interpreted as the character which binary value has this number. For example, 'a' has order 97, which is written 141 in base 8, so the string "\141" is the same as "a" (an octal digit is 0, ..., 7). So in the second string, "\0" is interpreted as the null character but "\03" is the character with number 3, while "\09" would mean a null character followed by the digit 9, because 9 is not an octal digit.

Because they can have a variable number of digits, the octal escaped sequences are not very useful. The better way is to represent non printable characters with hexadecimal escaped sequences like "\x61" which contain \x followed by exactly 2 hexadecimal digits (0, .., 9, a, .., f or A, .. F). So your examples would be better written

>>> "HuXiaoxiang\\0Nanjing University\x003 Years.".replace("\x00"," ")
'HuXiaoxiang\\0Nanjing University 3 Years.'
>>> "HuXiaoxiang \x00Nanjing University\x03 years.".replace("\x00"," ")
'HuXiaoxiang  Nanjing University\x03 years.'
>>> "\x0hello"
ValueError: invalid \x escape

The last example was rejected because \x expects exactly 2 hexadecimal digits.

commented: nice explanation +15

Aha, I grasp your point of "literal string". A big thank you to you!
Thx Gribouillis~ It is clear now

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.