A Python string does not have a single binary representation—each character is encoded as one to four UTF-8 bytes, so a Python text-to-binary routine must decide whether to emit Unicode code points, UTF-16 code units, or actual UTF-8 bytes, and whether to pad or trim bit groups. Most quick recipes in Python use format(ord(c), 'b') or bin(ord(c)), which return the bit pattern of a Unicode scalar value and drop leading zeros, so the result is rarely eight bits per character and rarely round-trips back to the original string. A more faithful approach encodes the string with UTF-8 first, then formats each byte as eight zero-padded bits; that is the same byte stream a network socket, file handle, or HTTP body would carry, and it is the form most text-to-binary tools actually print. Understanding which one you need prevents silent corruption when you copy the output into another program.

how to convert text to binary in python
Text to Binary in Python: Byte-Exact UTF-8 in Practice

Why Python's built-in tools give you variable-width output

Python makes it easy to write a one-liner that looks like binary output, but the most common patterns return inconsistent widths that do not match the byte stream other systems expect.

The fastest recipe, " ".join(format(ord(c), "b") for c in s), drops leading zeros entirely. The string "Hi" becomes 1001000 1101001, two groups of seven bits each, not eight. A downstream decoder has no way to tell whether 1001000 means the seven-bit value 72 or the eight-bit value 01001000, so the result cannot round-trip without extra metadata.

Switching to format(ord(c), "08b") pads each group to eight bits, which fixes the width problem, but introduces a different one. Python strings are sequences of Unicode code points, not UTF-8 bytes, so the string "é" (U+00E9) is treated as a single 16-bit value and prints as 11101001—an eight-bit group whose bits come from the Unicode scalar value 233 rather than the actual UTF-8 encoding 11000011 10101001. The same applies to every character outside the Basic Multilingual Plane: an emoji such as "" prints as a single 17-bit string from its scalar value 128512 rather than the four UTF-8 bytes.

The pattern that matches what a file, a socket, or an HTTP body actually carries is to encode the string first and then format every byte:

" ".join(f"{b:08b}" for b in s.encode("utf-8"))

That version is short, faithful, and exactly what the Text to Binary Converter implements with the browser's standards-based TextEncoder. For a side-by-side comparison of these recipes and the bytes they emit, see the byte-exact guide Convert Text to Binary Code: Byte-Exact UTF-8.

What byte-exact UTF-8 looks like for common characters

The four UTF-8 widths correspond to the high bits of the leading byte, and the same byte counts apply whether the encoding runs in Python, in JavaScript, or inside a browser-based tool.

CharacterUTF-8 byte countByte pattern
ASCII letter (A)101000001
Accented Latin (é)211000011 10101001
Currency sign (€)311100010 10000010 10101100
Supplementary emoji ()411110000 10011111 10011000 10000000

You can confirm each row in Python: len("A".encode("utf-8")) returns 1, len("é".encode("utf-8")) returns 2, len("€".encode("utf-8")) returns 3, and len("😀".encode("utf-8")) returns 4. If a binary converter's reported byte count disagrees with those numbers, it is encoding code points rather than bytes, and the result will not match the byte stream any modern protocol transmits.

Convert text to binary with the Text to Binary Converter

For quick verification, documentation, or visual inspection, running the conversion in the browser avoids the trial-and-error loop of tweaking format strings in the REPL:

  1. Open the Text to Binary Converter and select the Text to UTF-8 binary mode.
  2. Paste or type the text you want to encode, up to 20,000 UTF-16 code units in total.
  3. Select Convert and read the byte count shown under the output to confirm the size you expected.
  4. Cross-check the byte count against len(text.encode("utf-8")) in Python; identical numbers confirm the tool is emitting bytes, not code points.
  5. Copy the spaced binary into a destination that preserves ordinary spaces and line content—plain-text files, source code, terminal buffers, or markdown blocks all qualify.

This workflow is most useful when you need a clean visual record of what a Python string will look like on the wire, or when you want to verify a manually formatted bit string without writing test code.

Decode binary back to text with strict validation

The reverse direction is just as error-prone as the forward one, and a tolerant decoder hides more bugs than it catches. The Text to Binary Converter accepts only groups of exactly eight binary digits separated by one ordinary ASCII space, so prefixes such as 0b, commas, tabs, multiple spaces, seven- or nine-bit groups, and any leading or trailing whitespace all cause the decode to fail. That strictness is the point: when a downstream tool consumes an exact UTF-8 byte string, a parser that silently trims or pads groups will hide the mismatch.

To round-trip a value:

  1. Switch the tool to UTF-8 binary to text mode.
  2. Paste the byte groups, making sure there is exactly one space between groups and no whitespace at the start or end of the line.
  3. Select Convert. If the byte sequence is structurally well grouped but invalid UTF-8—a lone leading byte without its continuations, an overlong encoding, or a surrogate value—the tool returns a clear error rather than the Unicode replacement character.
  4. Compare the decoded string to the original in Python with original == decoded_text to confirm a clean round trip.

The underlying Web API is documented on MDN for both directions; the TextEncoder and TextDecoder fatal option explain how a strict decoder rejects malformed sequences without replacement.

Limits, errors, and pitfalls when copying the output

Two hard limits govern what the tool will accept. Encoding caps at 20,000 UTF-16 code units, so a supplementary emoji counts as two code units there but four UTF-8 bytes in the output. Decoding caps at 180,000 input characters, which is roughly 20,000 bytes when every group uses the standard eight-digit-with-space pattern.

A few pitfalls catch Python developers specifically:

  • Chat clients and rich-text editors collapse spaces. Slack, Teams, Word, and Google Docs may convert the single space between byte groups into no space, multiple spaces, or a line break, which then breaks strict decoding. Store binary in plain-text files, source code, or terminal buffers instead.
  • Trailing newlines change the byte count. A pasted paragraph that ends with \n encodes one extra byte (00001010). Strip whitespace in Python with s.rstrip("\n") if you do not want it in the binary.
  • Binary is not encryption. The output contains the same information as the input, in a more cumbersome format. Anyone with the same tool, or any UTF-8 decoder, can read it. For confidentiality, use AES or a password-based scheme rather than a binary representation.
  • Control bytes are real bytes. The byte 00001010 is a newline character and is encoded faithfully, the byte 00000000 is a NUL, and the byte 00010001 is device control 1. If a downstream parser rejects control bytes, filter them in Python before encoding instead of blaming the converter.

When two binary strings might differ, decode both with the tool and compare the resulting Unicode text; identical bytes produce identical characters, and the fatal decoder surfaces any divergence with a concrete error rather than a quiet replacement.

Related reading: Text to Hex: Command Line vs Online Compared.