Question

experts!

I'm trying to make a twitter client, with Twython. For now, i retrieve the command from the interactive interpreter.

In a test to upload a picture, i passed this string:

   "tweet photo: 'tweeted from python test' + path: 'C:\Users\akhya_000\Pictures\My Pictures\Bing.png"

But i found it invalid and resulted in this error:

SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 51-52: truncated \UXXXXXXXX escape

Can someone help me, what's wrong with the string? What should i do to fix it?

Was it helpful?

Solution

You want to escape the backslashes or use a raw string, since Python is seeing the \U and interpreting it as a Unicode escape sequence.

Escaping looks like this:

"tweet photo: 'tweeted from python test' + path: 'C:\\Users\\akhya_000\\Pictures\\My Pictures\\Bing.png"

A raw string, where Python will ignore escape sequences, looks like this:

r"tweet photo: 'tweeted from python test' + path: 'C:\Users\akhya_000\Pictures\My Pictures\Bing.png"
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top