vidhya9
vidhya9

Reputation: 27

How to insert a line break within the for loop of a print statement using python

I am using the following code to print the list of friends in twitter

users = api.friends()
print([u.name for u in users])

I get the following output

['The New York Times', 'BBC Breaking News', 'Oprah Winfrey']

But I want something like this

['The New York Times',
'BBC Breaking News',
'Oprah Winfrey' ]

Upvotes: 1

Views: 4323

Answers (4)

Pradeep Singh
Pradeep Singh

Reputation: 81

Hope below works

import pprint users = api.friends() pprint.pprint([u.name for u in users], width=1)

Upvotes: 1

lawful_neutral
lawful_neutral

Reputation: 673

You can also use

print('\n'.join([u.name for u in users]))

Upvotes: 2

cadef
cadef

Reputation: 36

Do you need to keep the list brackets, quotes, and commas? If you just want to print the items one per line, you could do something like:

print(*[u.name for u in users], sep='\n')

Or you could loop and print each item individually:

for u in users:
    print(u.name)

Upvotes: 2

BetaDev
BetaDev

Reputation: 4674

Just break the code, Write loop and then print each name

for u in users:
       print u.name
       print ""

Upvotes: 2

Related Questions