Sagar Chand
Sagar Chand

Reputation: 260

Enter "\\\" in a string in python

How to insert "\\\" in a string in python. It seems to give Error

SyntaxError: EOL while scanning string literal

Upvotes: 0

Views: 312

Answers (3)

Chamoda De silva
Chamoda De silva

Reputation: 63

We can insert \ from single print('\\')

so you wanted to type \\\\\\ then use six '\\' to get '\\\\\\' such as

print('\\\\\\\\\\\\')

Upvotes: 1

Joe Ferndz
Joe Ferndz

Reputation: 8508

You can use string concat option to get as many \ as you want.

If you want 3 \ then you can do:

print ('\\'*3)

If you want 8 \, then you can just do:

print ('\\'*8)

This will eliminate the need to keep typing \\\\ many times.

Here's a way to add it to a variable and use it for printing later:

>>> x = 'This is an example for '+'\\'*3+' 3 backslashes'
>>> print (x)
This is an example for \\\ 3 backslashes

Upvotes: 1

Anton vBR
Anton vBR

Reputation: 18916

Yes because \ means break in Python.

\\ equals \but if you use 3 you are breaking something again.

Upvotes: 0

Related Questions