List comprehension.

[c + '\0' for c in S]

But it smells like you want UTF-16LE instead.

u'teststring'.encode('utf-16le')
Answer from Ignacio Vazquez-Abrams on Stack Overflow
๐ŸŒ
Reddit
reddit.com โ€บ r/cs50 โ€บ why do i need to add null character to create a valid ascii text?
r/cs50 on Reddit: Why do I need to add null character to create a valid ASCII text?
March 5, 2021 -

My output looked correct however the check50 said output was not a valid ASCII text. After googling I fixed it by adding the null character to the end of the output array.

I understand that the null character is how C tells where the string ends, however I still don't fully understand why it was needed for my program, since the output that I see still looks the same whether or not I have that character. Since my output is in a char array so C already knows the size of that array.

Is someone able to explain why adding this character was needed? Is it just something that the check50 checks arbitrarily to qualify it as "valid ASCII text"?

๐ŸŒ
ASCII Code
ascii-code.com โ€บ character โ€บ โ€
Null character - ASCII Code
In some cases, it is referred to as the NUL character. Here are some key details: In ASCII and Unicode, the null character is defined to be U+0000, meaning it is represented in Unicode as the number 0.
๐ŸŒ
Medium
medium.com โ€บ @amey0x โ€บ null-byte-vs-null-terminator-vs-null-character-f25fc0ebc3f6
Null Byte vs Null terminator vs Null character ๐Ÿ˜ตโ€๐Ÿ’ซ | by Aman singh | Medium
October 27, 2023 - Here are some examples of how the null character is used in code: # Python string my_string = "Hello, world!" # Null character at the end of the string print(my_string[-1]) # Output: 0

List comprehension.

[c + '\0' for c in S]

But it smells like you want UTF-16LE instead.

u'teststring'.encode('utf-16le')
Answer from Ignacio Vazquez-Abrams on Stack Overflow
๐ŸŒ
Python
bugs.python.org โ€บ issue35714
Issue 35714: Document that the null character '\0' terminates a struct format spec - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/79895
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null_character
Null character - Wikipedia
1 week ago - The null character is a control character with the value zero. Many character sets include a code point for a null character โ€“ including Unicode (Universal Coded Character Set), ASCII (ISO/IEC 646), Baudot, ITA2 codes, the C0 control code, ...
๐ŸŒ
Quora
quora.com โ€บ How-can-I-use-the-null-terminated-characters-in-a-Python-string
How to use the null-terminated characters in a Python string - Quora
Answer: I suspect that you mean, how can I convert null-terminated strings to Python strings? In short, use the [code ]rstrip[/code] method for the string. Create a null-terminated string and apply [code ]rstrip[/code] to it. By default, [code ]rstrip[/code] will strip away new-line, carriage-r...
Find elsewhere
๐ŸŒ
GitHub
github.com โ€บ pybind โ€บ pybind11 โ€บ issues โ€บ 2239
How to return a string containing null characters to Python ยท Issue #2239 ยท pybind/pybind11
May 2, 2020 - Issue description I want to know how to return a string containing null characters to Python. Reproducible example code PYBIND11_MODULE(example, m) { m.def("return_bytes", []() { return py::bytes("Hello\x00\x00World"); // return py::str(...
Published ย  Jun 02, 2020
๐ŸŒ
Python
bugs.python.org โ€บ issue20368
Issue 20368: Tkinter: handle the null character - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/64567
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ c++ โ€บ g-fact-72
ASCII NULL, ASCII 0 ('0') and Numeric literal 0 - GeeksforGeeks
July 23, 2025 - # Python code equivalent to the ... all its bits set to logic 0, which means each byte will be filled with a NULL character (\ 0)....
๐ŸŒ
SSOJet
ssojet.com โ€บ home โ€บ null 0 in python
Null (\0) in Python | SSOJet
March 1, 2025 - The null character, represented as \0 in Python, is simply the ASCII NUL (0) byte.
๐ŸŒ
Python.org
discuss.python.org โ€บ python help
How to get null terminated strings from a buffer? - Python Help - Discussions on Python.org
May 19, 2023 - Hi all, I am writing a gui library in Python with ctypes. So far so good. Currently I am writing the common dialog boxes like file open, file save and folder browser. Windows will allow us to open more than one file with OFN_ALLOWMULTISELECT flag. But if we use this flag, the lpstrFile member of the OPENFILENAMEW struct behaves quite differently.
๐ŸŒ
Wikipedia
en.wikipedia.org โ€บ wiki โ€บ Null-terminated_string
Null-terminated string - Wikipedia
March 25, 2025 - Null-terminated strings require that the encoding does not use a zero byte (0x00) anywhere; therefore it is not possible to store every possible ASCII or UTF-8 string. However, it is common to store the subset of ASCII or UTF-8 โ€“ every character except NUL โ€“ in null-terminated strings.
๐ŸŒ
Python
bugs.python.org โ€บ issue28350
Issue 28350: Interning string constants with null character - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/72536
Top answer
1 of 1
20

ASCII is only for [printable] character[s]

Not quite: in addition to printable characters, ASCII also includes a number of control characters.

ASCII code 0 (NUL) is one such control character.

To quote Wikipedia:

ASCII reserves the first 32 codes (numbers 0-31 decimal) for control characters: codes originally intended not to represent printable information, but rather to control devices (such as printers) that make use of ASCII, or to provide meta-information about data streams such as those stored on magnetic tape. For example, character 10 represents the "line feed" function (which causes a printer to advance its paper), and character 8 represents "backspace".

These days, the NUL character is most frequently used to signify the end of a character string in C. Its original purpose, however, was different:

The original meaning of this character was like NOP -- when sent to a printer or a terminal, it does nothing (some terminals, however, incorrectly display it as space). When electromechanical teleprinters were used as computer output devices, one or more null characters were sent at the end of each printed line to allow time for the mechanism to return to the first printing position on the next line. On punched tape, the character is represented with no holes at all, so a new unpunched tape is initially filled with null characters, and often text could be "inserted" at a reserved space of null characters by punching the new characters into the tape over the nulls.

๐ŸŒ
Reddit
reddit.com โ€บ r/json โ€บ using the null character as a delimiter in a json string
r/json on Reddit: Using the null character as a delimiter in a json string
August 5, 2021 -

An representative example of the JSON I would like to create is:

[
    {
        "aaaa": {
            "bbbb": [
                {
                    "cccc": "eeee",
                    "dddd": "ffff\u0000gggg"
                }
            ]
          }
    }
]

What I would like to be able to do is separate ffff and gggg will the null character as a delimiter.

Is this valid JSON according to the spec?

Googling turned up little information. I did find:

https://jansson.readthedocs.io/en/1.2/conformance.html

which says:

JSON strings are mapped to C-style null-terminated character arrays, and UTF-8 encoding is used internally. Strings may not contain embedded null characters, not even escaped ones.

For example, trying to decode the following JSON text leads to a parse error:

["this string contains the null character: \u0000"]

All other Unicode codepoints U+0001 through U+10FFFF are allowed.

and this seems to indicate that ffff\u0000gggg is not legal.

However, based on my tests, ffff\u0000gggg seems to be parsed correctly by both Python and Javascript parsers correctly. However, I am not sure if I am getting lucky or what exactly the right answer is.

Can anyone clear this up?

๐ŸŒ
Python
bugs.python.org โ€บ issue6011
Issue 6011: python doesn't build if prefix contains non-ascii characters - Python tracker
This issue tracker has been migrated to GitHub, and is currently read-only. For more information, see the GitHub FAQs in the Python's Developer Guide ยท This issue has been migrated to GitHub: https://github.com/python/cpython/issues/50261