The below code prints the emoji like, this 😂 :
print('\U0001F602')
print('{}'.format('\U0001F602'))
However, If I use \ like the below, it prints \U0001F602
print('\{}'.format('U0001F602'))
Why the print('\{}'.format()) retunrs \\, not a escape character, which is \?
I have been checking this and searched in Google, but couldn't find the proper answer.
'\U0001F602'is evaluated during compile time and a string literal starting with\Uhas a special meaning for the compiler.'\{}'.format('U0001F602')is evaluated during runtime (and should be written with an escaped backslash as'\\{}'.format('U0001F602')anyway)\{isn't a valid escape sequence, and Python handles that by treating the backslash literally. The now-linked duplicate is the best reference I could find for this problem - in more recent versions of Python, you will get such a warning message on this code.-Wdflag for Python to see this.)