How do I converted a null (0) terminated string to a Python string?
John Machin
sjmachin at lexicon.net
Thu Sep 14 11:26:27 EDT 2006
Michael wrote:
> John,
>
> Since I'm new to Python, I'm having trouble understanding what this
> means (see below). Would appreciate any help.
>
> if strg[-1] == "\0":
If the last (i.e index -1) byte in the string equals ASCII NUL:
> strg = strg[:-1]
then take a slice of the string from the start up to but not including
the last byte and assign that to "strg"
In other words, if the last byte of strg is NUL, throw it away.
The truly paranoid would code that as
if strg and strg[-1] etc etc
so that it wouldn't blow up if strg is empty -- strange things can
happen when you are reading other people's data :-)
Perhaps you should work through the tutorial; all the above concepts
are treated in this section:
http://docs.python.org/tut/node5.html#SECTION005120000000000000000
HTH,
John
More information about the Python-list
mailing list