2 Replies - 142 Views - Last Post: 23 March 2019 - 02:47 PM Rate Topic: -----

#1 miljan_python   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-November 18

Unpacking a list of object references in format method

Posted 23 March 2019 - 11:13 AM

Hi,

As the title said, I'm trying to do the following. Tried at least 10 different ways, non worked.

    l = [1, 2, 3]
    print(len(l)*"{}".format(*l))



I always get the first (zero-index element) printed. Actually 1, 2, 3 will be some object references...
Is This A Good Question/Topic? 0
  • +

Replies To: Unpacking a list of object references in format method

#2 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2736
  • View blog
  • Posts: 4,376
  • Joined: 21-June 11

Re: Unpacking a list of object references in format method

Posted 23 March 2019 - 12:31 PM

The . has higher precedence than *, so you're calling format on a string with a single pair of braces and then multiplying the result. You need to add parenthesis around the multiplication.
Was This Post Helpful? 1
  • +
  • -

#3 miljan_python   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 21
  • Joined: 25-November 18

Re: Unpacking a list of object references in format method

Posted 23 March 2019 - 02:47 PM

Thanks. Cheers man :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1