Home python What is the reason SyntaxError: unexpected character after line continuation character

What is the reason SyntaxError: unexpected character after line continuation character

Author

Date

Category

Put an iterator for every 10 values ​​a new line:

if i == 10:
  ls.append (\ n)

Error:

ls.append (\ n)
        ^
SyntaxError: unexpected character after line continuation character

How can I do it correctly / fix a bug in python 2.7?

PS: Output of such a program: [0, 0, 0, 0, 0, 0, 0, 0, 0, -10, -10, -10, -10, -10, -10, -10, – 10, -10, -10, -20, -20, -20, -20, -20, -20, …]
Here’s the code itself:

ls = []
i = 0
for z in range (1,1000,1):
  i + = 1
  x = z #; print x
  inv_x = int (str (x) [:: - 100]) #; print inv_x
  a = inv_x-x #; print a
  inv_a = int (str (a) [:: - 100]) #; print inv_a
  b = a + inv_a #; print b
  ls.append (b)
print ls

Answer 1, authority 100%

If you want to concatenate list elements with a separate symbol:

text = '\ n'.join ([' a ',' b ',' c '])

If you need to concatenate a list that already contains ‘\ n’ elements:

text = '' .join (['a', 'b', '\ n', 'c' , 'd'])

Answer 2

Remove spaces after adding “\ n)”
If it’s pycharm. Alt + ctrl + L


Answer 3

If you need to break the text into lines for easy viewing, it is better to use the functions from the textwrap module , then the word wrap will be correct. Example:

from textwrap import fill
print (fill ('qwerty asfgh zxcvhb', width = 15))

Output:

qwerty asfgh
zxcvhb

If the “words” are too long to fit in the specified width, they will be split into chunks of unspecified width:

print (fill ('11111111111111111111111111111111111111111111111111111', width = 10))

Output:

1111111111
1111111111
1111111111
1111111111
1111111111
111

Answer 4

As is:

ls.append (\ n)

Right:

ls.append ('\ n')

The .append () function takes a string. You forgot to put the quotes, and therefore your \ n is just a backslash and a non-existent n name, not a string.

Programmers, Start Your Engines!

Why spend time searching for the correct question and then entering your answer when you can find it in a second? That's what CompuTicket is all about! Here you'll find thousands of questions and answers from hundreds of computer languages.

Recent questions