Home > Mobile >  Can't do ASCII with 'u' character in python
Can't do ASCII with 'u' character in python

Time:07-27

I'm trying to do an ascii image in python but gives me this error

File "main.py", line 1
    teste = print('''
                  ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 375-376: truncated \UXXXXXXXX escape

And I think it's because of the U character, why that happened, is any way to solve this? ASCII image

CodePudding user response:

You've got \U in your string, which is being interpreted as the beginning of a Unicode ordinal escape (it expects it to be followed by 8 hex characters representing a single Unicode ordinal).

You could double the escape, making it \\U, but that would make it harder to see the image in the code itself. The simplest approach is to make it a raw string that ignores all escapes save escapes applied to the quote character, by putting an r immediately before the literal:

teste = print(r'''

Note the r immediately after the (, before the '''.

  • Related