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

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

Mistake:

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

How can I do this correctly / fix an error in python 2.7?

PS: The 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 is 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 

Closed due to the fact that the question is too common for participants insolor , D-side , Grundy , Abyx , user194374 Mar 27 '16 at 6:13 .

Please correct the question so that it describes the specific problem with sufficient detail to determine the appropriate answer. Do not ask a few questions at once. See “How to ask a good question?” For clarification. If the question can be reformulated according to the rules set out in the certificate , edit it .

  • ls.append ('\ n') - dakiesse
  • Put an error message in the header, because (again) your task is not directly related to the error (that is, "how to fix SyntaxError" has nothing to do with "how to insert a new line every 10 characters" - this is a completely different question) . - jfs
  • one
  • '\ n' - not suitable - you need a new line every 10 values ​​(for easy viewing). - ALPHA
  • @ ALPHA, write what exactly should be the result? List printed on 10 numbers per line? This is certainly not done by inserting '\ n' into the list. Write exactly how you want to see the result. - insolor

4 answers 4

If you want to concatenate list elements with the indication separate symbol:

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

If you need to concatenate a list in which there are already '\ n' elements:

 text = ''.join(['a', 'b', '\n', 'c', 'd']) 
  • 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 - ALPHA
  • you need to put this code in your question - dakiesse
  • @ALPHA, add this code to the question. - insolor

Remove spaces after adding "\ n)" If it is pycharm. Alt + ctrl + L

  • there are no spaces - alpha
  • @ALPHA: ls.append('\n') - gil9red

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)) 

Conclusion:

 qwerty asfgh zxcvhb 

If the "words" are too long to fit in a given width, they will be broken into pieces of an unspecified width:

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

Conclusion:

 1111111111 1111111111 1111111111 1111111111 1111111111 111 
  • Only there is not text but numbers - ALPHA
  • @ALPHA, add an example of the text you need to break into the question. - insolor
  • 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 - ALPHA
  • @ALPHA, click the "edit" button in question, and paste the code there, but do not scatter it by commenting. - insolor

As it is:

 ls.append(\n) 

How to:

 ls.append('\n') 

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