Search code examples
pythoncomments

How to add inline comments to multiline string assignments in python


How to add comments to multiline assignments in python, as is possible in C with the syntax:

char sc[] = "\x31\xdb"                  /* xor %ebx, %ebx       */
            "\x31\xc9"                  /* xor %ecx, %ecx       */
            "\xb8\x46\x00\x00\x00"      /* mov $0x46, %eax      */
            "\xcd\x80"                  /* int $0x80            */
            "\x31\xdb"                  /* xor %ebx, %ebx       */
            "\xb8\x01\x00\x00\x00"      /* mov $0x1, %eax       */
            "\xcd\x80";                 /* int $0x80            */

but the same in python, using escaped line breaks

sc = "\x31\xdb" \   # xor %ebx, %ebx
     "\x31\xc9" \   # xor %ecx, %ecx
     "…"

Solution

  • You can write

    sc = ("\x31\xdb"      # xor %ebx, %ebx
          "\x31\xc9"      # xor %ecx, %ecx
          "…")
    

    if you want.