0

I transfer form JAVA and pretty new in python.I knew python is a convenience language. Then I consider if or not a way I could use in print(''.format()) function to print a list, which I insert couple of word in between value.

I have already tried print(''.format(for i in list)) as simple example showed below:

movies.append(1)
movies.append('The Shawshank Redemption')
movies.append('5 star')
movies.append('whatever')
movies.append('www.google.com')
print('''
    The NO.{} Movie is :{}
    Rating:{}
    Quote:{}
    Link:{}
    '''.formate(for i in movie)

of course, it shows error invalid syntax at last statement.

6
  • 1
    A list is not the ideal data structure here, you would have better luck with a dictionary. movies = [{'title': 'The Shawshank Redemption', 'rating': '5 star', 'quote':'whatever', 'link': 'www.google.com'}] You aren't using .format() correctly. It is for formating a single string with a list of input arguments. You could use it in a loop, but not as a loop. Commented Aug 15, 2019 at 0:43
  • 1
    .format(*movie) Commented Aug 15, 2019 at 0:47
  • 1
    The syntax error is that you are missing a closing parenthesis ) (also format not formate) Commented Aug 15, 2019 at 0:55
  • @juanpa.arrivillaga Good call, forgot about the list unpacking operator. That would work well here. Commented Aug 15, 2019 at 1:16
  • 1
    @campellcl still good comment. You remind me to use the dictionary in this case.thx Commented Aug 15, 2019 at 2:32

2 Answers 2

2

for i in movies is a for statement, so it expects to have a block of code.

What you want is to get every item for item in movies, and then pass each item to the function, so you should do this instead

print('''
    The NO.{} Movie is :{}
    Rating:{}
    Quote:{}
    Link:{}
    '''.format(*(i for i in movie)) # '*' unpacks the generator, so instead of passing a whole object to the function, you pass every object in the generator as an argument

Note that you can actually pass the unpacked list to format like so

print('''
    The NO.{} Movie is :{}
    Rating:{}
    Quote:{}
    Link:{}
    '''.format(*movies) # '*' unpacks the generator, so instead of passing a whole object to the function, you pass every object in the generator as an argument
Sign up to request clarification or add additional context in comments.

Comments

1

*movies unpack argument lists

movies = []
movies.append(1)
movies.append('The Shawshank Redemption')
movies.append('5 star')
movies.append('whatever')
movies.append('www.google.com')
print('The NO.{}\nMovie is :{}\nRating:{}\nQuote:{}\nLink:{} '.format(*movies))

Output is

The NO.1
Movie is :The Shawshank Redemption
Rating:5 star
Quote:whatever
Link:www.google.com 

1 Comment

You are also right. But I can only accept one. :( Thx anyway

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.