0

Python 3

I want to send a binary string over a socket. I am using

socket.send(b"Hello from {0}",client_id)

however I find the it is not properly substitution clientid.

what am I doing wrong? I do I accomplish this in a binary string

liv2hak
  • 14,472
  • 53
  • 157
  • 270

1 Answers1

1

{0} is a placeholder for the str.format method, which you should be using, but bytes objects do not have a format method; instead, you can format it as a string first and then convert it to bytes with the encode method:

socket.send("Hello from {0}".format(client_id).encode())
blhsing
  • 91,368
  • 6
  • 71
  • 106