[Tutor] Puzzled again

Peter Otten __peter__ at web.de
Wed Aug 3 19:11:59 CEST 2011

Richard D. Moores wrote:

> I wrote before that I had pasted the function (convertPath()) from my
> initial post into mycalc.py because I had accidentally deleted it from
> mycalc.py. And that there was no problem importing it from mycalc.
> Well, I was mistaken (for a reason too tedious to go into). There WAS
> a problem, the same one as before.

Dave was close, but Steven hit the nail: the string r"C:\Users\Dick\..." is 
fine, but when you put it into the docstring it is not a raw string within 
another string, it becomes just a sequence of characters that is part of the 
outer string. As such \U marks the beginning of a special way to define a 
unicode codepoint:

>>> "\U00000041"

As "sers\Dic", the eight characters following the \U in your docstring, are 
not a valid hexadecimal number you get an error message.

The solution is standard procedure: escape the backslash or use a rawstring:


>>> """yadda r"C:\Users\Dick\..." yadda"""
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in 
position 10-12: truncated \UXXXXXXXX escape


>>> """yadda r"C:\\Users\Dick\..." yadda"""
'yadda r"C:\\Users\\Dick\\..." yadda'

Also correct:

>>> r"""yadda r"C:\Users\Dick\..." yadda"""
'yadda r"C:\\Users\\Dick\\..." yadda'

More information about the Tutor mailing list