4

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.

AfterFray
  • 1,751
  • 3
  • 17
  • 22
  • 1
    `'\U0001F602'` is evaluated during compile time and a string literal starting with `\U` has a special meaning for the compiler. `'\{}'.format('U0001F602')` is evaluated during runtime (and should be written with an escaped backslash as `'\\{}'.format('U0001F602')` anyway) – Matthias Oct 07 '21 at 06:07
  • See [Process escape sequences in a string in Python](https://stackoverflow.com/questions/4020539) to solve the problem of turning the actual backslash, followed by capital U etc. into the emoji. As for the *question that was actually asked*, it's simple: `\{` **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. – Karl Knechtel Aug 07 '22 at 04:50
  • (Warnings are disabled by default; you would need the `-Wd` flag for Python to see this.) – Karl Knechtel Aug 07 '22 at 04:56

2 Answers2

6

Referring to String and Bytes literals, when python sees a backslash in a string literal while compiling the program, it looks to the next character to see how the following characters are to be escaped. In the first case the following character is U so python knows its a unicode escape. In the final case, it sees {, realizes there is no escape, and just emits the backslash and that { character.

In print('\{}'.format('U0001F602')) there are two different string literals '\{}' and 'U0001F602'. That the first string will be parsed at runtime with .format doesn't make the result a string literal at all - its a composite value.

tdelaney
  • 73,364
  • 6
  • 83
  • 116
1
>>> print('\{}'.format('U0001F602'))
\U0001F602

This is because you are giving {} as an argument to .format function and it only fills value inside the curly braces.

ANd it is printing a single \ not double \

Satyam Shankar
  • 250
  • 2
  • 12
  • Hi, you mentioned that i"t is printing a single \ not double \". Then why `('\{}'.format('U0001F602')) == ('\\{}'.format('U0001F602'))` returns **True** ? – AfterFray Oct 07 '21 at 06:15
  • You have used '==' operator, that operator checks whether the LHS = RHS , and if yes it returns true, and both return the same thing – Satyam Shankar Oct 07 '21 at 06:21
  • Understand it this way, – Satyam Shankar Oct 07 '21 at 06:22
  • When you use \\ it thinks the first backslash is the begining of an excape sequence and \\ prints a \ while a single \ is treated as a string – Satyam Shankar Oct 07 '21 at 06:23
  • Ok, I got it, Thank you – AfterFray Oct 07 '21 at 06:25
  • Main point with the backslash is that it is an escape character. `"\n"` is not a backslash followed by an "n" but a newline, `"\t"` is a tab and so on. If you want to have the backslash itself in a string you have to escape it: `"\\"`. But why does it work here? Python sees the escape character and checks the next character which is a `{`. Now `\{` isn't a valid escape sequence so Python backtracks and assumes that this backslash shouldn't be part of an escape sequence but a standalone backslash. – Matthias Oct 07 '21 at 09:06
  • Not escaping the escape character itself might lead to some subtle bugs. Check the result of '\\m' == '\m' vs. '\\n' == '\n'. And check what happens when you do `print('\\')` and what happens when you do `print('\')`. – Matthias Oct 07 '21 at 09:08