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.

Chanhee
  • 1,649
  • 2
  • 13
  • 20
  • 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

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
  • 63,514
  • 5
  • 71
  • 101
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 \

  • Hi, you mentioned that i"t is printing a single \ not double \". Then why `('\{}'.format('U0001F602')) == ('\\{}'.format('U0001F602'))` returns **True** ? – Chanhee 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 – Chanhee 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