Python Forum
misunderstanding of format in print function
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
misunderstanding of format in print function
#1
Hello,
I'm using Python 3.7.0 32-bit and wanted to print some variables with the format syntax
The variables are :
client_address: ('192.168.167.200', 49706)
message['FrameNumber']: 396
The code:
print('Accepted new connection from {}:{} {}'.format(*client_address,client_address[1],message['FrameNumber']))
gives the result :
Output:
Accepted new connection from 192.168.167.200:49706 49706
Why is there 2 times the value 49706 ?

while in two lines the result is what I expected :
print('Accepted new connection from {}:{}'.format(*client_address,client_address[1]))
print('frame:{}'.format(message['FrameNumber']))
Output:
Accepted new connection from 192.168.167.200:49706 frame:396
Reply
#2
Because you unpack and have four values which will not fit into three placeholders so the last one will be skipped. Observe:

>>> client_address = ('192.168.167.200', 49706) 
>>> message = 396                                                                          
>>> print(*client_address, client_address[1], message)                                     
192.168.167.200 49706 49706 396
In two lines you skip the third value in first line. So if you look for solution skip 'client_address[1]' in your original code and everything should be fine.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply
#3
*client_address unpacks the tuple into its individual elements. This way format(*client_address,client_address[1],message['FrameNumber']) is same as format('192.168.167.200', 49706, 49706, 396)
You have just 3 placeholders thus the last element (396) is simply ignored

when on 2 lines it just ignores the second 49706.

In other words, change your code to .format(*client_address, message['FrameNumber'])
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  print doesnt work in a function ony 2 314 Mar-11-2024, 12:42 PM
Last Post: Pedroski55
  How to print variables in function? samuelbachorik 3 917 Dec-31-2022, 11:12 PM
Last Post: stevendaprano
  How to print the output of a defined function bshoushtarian 4 1,321 Sep-08-2022, 01:44 PM
Last Post: deanhystad
  .format function Menthix 6 1,688 Mar-12-2022, 10:32 AM
Last Post: stevendaprano
  use of format function barryjo 3 1,678 Feb-01-2022, 08:07 AM
Last Post: menator01
  Why does absence of print command outputs quotes in function? Mark17 2 1,393 Jan-04-2022, 07:08 PM
Last Post: ndc85430
  return vs. print in nested function example Mark17 4 1,757 Jan-04-2022, 06:02 PM
Last Post: jefsummers
  Date format and past date check function Turtle 5 4,281 Oct-22-2021, 09:45 PM
Last Post: deanhystad
  Print first day of the week as string in date format MyerzzD 2 2,033 Sep-29-2021, 06:43 AM
Last Post: MyerzzD
  output correction using print() function afefDXCTN 3 11,111 Sep-18-2021, 06:57 PM
Last Post: Sky_Mx

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020