Is there a formatted byte string literal in Python 3.6+?

前端 未结 5 1330
走了就别回头了
走了就别回头了 2020-12-17 07:29

I\'m looking for a formatted byte string literal. Specifically, something equivalent to

name = \"Hello\"
bytes(f\"Some format string {name}\")
5条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-17 08:19

    I managed to format a literal in order to send it through a socket:

    msgIndex = 0
    while(IsSomeCondition()):
      msgIndex += 1
      sock.sendto(
        b"msg number %s" % bytes(str(msgIndex), encoding="utf8"),
        (receiverIP, receiverPort)
      )
    # --> b'msg number 1'
    # --> b'msg number 2'
    # --> b'msg number 3'
    

    I'm using Python 3.8.5.

提交回复
热议问题