Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Add null character to string in python

Tags:

python

I have list as:

['t','e','s','t','s','t','r','i','n','g']

How to add null character after each string t, e, s, t, s, t, r, i, n, g?

like image 622
Louis.CLast Avatar asked Aug 18 '12 05:08

Louis.CLast


People also ask

How do you add a null character to a string?

1 Answer. Show activity on this post. Isspace function will search the string only till it encounters the NULL(\0) character. hence compare the string elements with the space (or ASCII value) to recognize the space and replace it with a '\0' character.

How do you write a null character in Python?

Python uses the keyword None to define null objects and variables. While None does serve some of the same purposes as null in other languages, it's another beast entirely. As the null in Python, None is not defined to be 0 or any other value. In Python, None is an object and a first-class citizen!

Does string have null character in Python?

There is no end-of-string character in Python, at least not one that is exposed and it would be implementation dependent. String objects maintain their own length and it is not something that you need to be concerned with.

What does \0 mean in a Python string?

It's an indicator to the format method that you want it to be replaced by the first (index zero) parameter of format. ( eg "2 + 2 = {0}".format(4) )


1 Answers

List comprehension.

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

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

u'teststring'.encode('utf-16le')
like image 100
Ignacio Vazquez-Abrams Avatar answered Oct 21 '22 08:10

Ignacio Vazquez-Abrams