-2

append new line to a string with in an array

result = []

str = ""

str = str + " this is my result for..."

result.append([x, y, str])

output will be :

[1, 2, this is my result for...], [12, 297, this is my result for...], [13, 42, this is my result for...], [34, 42, this is my result for...],[1345, 332, this is my result for...], [341, 244, this is my result for...]

But i want the output to be :

[1, 2, this is my result for...], 
[12, 297, this is my result for...], 
[13, 42, this is my result for...], 
[34, 42, this is my result for...],
[1345, 332, this is my result for...], 
[341, 244, this is my result for...]
abcde
  • 85
  • 1
  • 2
  • 7
  • 2
    `for line in result: print line`? – jonrsharpe Nov 10 '15 at 14:00
  • Possible duplicate of [New line Python](http://stackoverflow.com/questions/11497376/new-line-python) – Xavjer Nov 10 '15 at 14:01
  • result here is an array. result = [] – abcde Nov 10 '15 at 14:19
  • To elaborate on what @jonrsharpe suggested. `result` is a list of lists and `print result` will print the list in one line. It is not a string and does not have newline characters. print adds a newline character after each call so you can loop through your list and print each element one at a time. – SirParselot Nov 10 '15 at 14:40
  • yes you are right. after every append in the list i want a new line so that the desired output is displayed as above – abcde Nov 10 '15 at 14:49

1 Answers1

2

You can use pprint to get a formatted output.

result = [
    ['this is my result for...', 1, 2],
    ['this is my result for...', 12, 297],
    ['this is my result for...', 13, 42]]

from pprint import pprint
pprint result

[['this is my result for...', 1, 2],
['this is my result for...', 12, 297],
['this is my result for...', 13, 42]]

Or loop over the results as suggest by jonrsharpe:

result = [
    ['this is my result for...', 1, 2],
    ['this is my result for...', 12, 297],
    ['this is my result for...', 13, 42]]

for line in result: print line

['this is my result for...', 1, 2]
['this is my result for...', 12, 297]
['this is my result for...', 13, 42]

Cyrbil
  • 6,341
  • 1
  • 24
  • 40