How do I converted a null (0) terminated string to a Python string?

Fredrik Lundh fredrik at pythonware.com
Thu Sep 14 12:36:29 EDT 2006


Marc 'BlackJack' Rintsch wrote:

> I would spell it:
> 
> if strg.endswith('\0'):
>     strg = strg[:-1]

unless you're in a hurry; startswith and endswith are horribly 
inefficient compared to ordinary indexing/slicing.

(as I've pointed out elsewhere, even "s[:len(t)] == t" is usually faster 
than "s.startswith(t)" for short prefixes, where "short" is a lot longer 
than you may think).

</F>




More information about the Python-list mailing list