How to use print for special Python characters?

Asked

Viewed 62 times

0

When trying to use print for a special character, I get the following error:

print('\')

  File "<ipython-input-38-eaac87876c3b>", line 1
    print('\')
              ^
SyntaxError: EOL while scanning string literal
  • PS: the special character is ( \ )

  • 2

    Thiago, good morning! Add another , that way print('\\'). Hug!

2 answers

2

The character \ has special meaning in strings: it is used for escape sequences (for example, \n represents the line break, \t represents the GRT, etc).

And in case, \' represents the character itself ', since that’s how I do for that character ' is represented in a string bounded by itself '. That is to say, print('\'') will print a '.

That’s why it was wrong, because the first ' indicates the beginning of the string, and inside it \', which means "a character '", and since there is no closure of the string, error occurs.

If you want to print your own character \, this should be written as \\, thus:

print('\\')

This will print the character \.

1

Try using Unicode table codes to print this or any other special character:

print('\u005C')

You can check the Unicode tables of various special characters here:

Browser other questions tagged

You are not signed in. Login or sign up in order to post.