Is there a way to declare a char array of a fixed size in python as in C for example
char myArray[100]
I also want to initializa all the characters with NULL.
3 Answers
You can't have a fixed size string. (Python doesn't work like that). But you can easily initialize a string to 100 characters:
myArray = "\0" * 100
1 Comment
std::string exists for a reason), and arguably not even in C (there is no first-class object representing the string; it's an abstraction over data that is blindly iterated over and wasn't necessarily ever within an array). Contrarily, a Python string isn't an array of any kind, certainly not of characters: a Python string is immutable and its elements are more strings (and there is no separate character type).You can use array (an array in python have fixed type signature, but not fixed size):
>>> import array
myArray = array.array('c', ['\0' for _ in xrange(100)])
>>> myArray
array('c', '\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00')
>>> myArray[45]
'\x00'
Notice that i initialize the default to '\0' couse in python you must initialize it with a value, and array have not fixed size (it is dynamic) but this will do.
Another option is to initialize the array and appende the values later, so instead of full of NULL (None in python) it will be just empty and grow at your will:
>>> a = array.array('c',)
>>> a
array('c')
>>> a.append('c')
>>> a
array('c', 'c')
Another way is to use numpy.chararray:
import numpy as np
myArray=np.chararray(100)
myArray[:]=0 #NULL is just a zero value
Results:
>>> myArray
chararray([b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0',
b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0', b'0'],
dtype='|S1')
buf = 'test' myFunc(buf)Now I expected the string to grow automatically in length depending on the output provided by the function, but it seems that the length of the string is fixed by declaration. So in this case it is 4, and if fo rexample the function writes "thisisanoutput" to it, after the function returns the value of buf is "this".NULLmeans a null pointer constant and is not a value that can be used for initializing achar(use0or'\0'). Second, Python doesn't have variable declarations at all, nor does it have a separate character type vs. strings. Third, Python's lists automatically resize (likestd::vector) so there is no need to "reserve" or "pre-size" memory in any ordinary circumstance. Fourth (going by the accepted answer) a Python string is not any kind of array and cannot be mutated.