3

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.

5
  • Maybe this thread helps: stackoverflow.com/questions/6142689/… Commented Jan 8, 2016 at 11:09
  • Or you can check this one Commented Jan 8, 2016 at 11:10
  • 1
    What is the benefit you're expecting? Commented Jan 8, 2016 at 11:10
  • I have a written a module in C, which has a function that writes something to an output string passed as an argument. I declare the string in python before passing it to the function such as: 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". Commented Jan 8, 2016 at 11:15
  • There are many misconceptions here. First off, the original C++ isn't even understood correctly: NULL means a null pointer constant and is not a value that can be used for initializing a char (use 0 or '\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 (like std::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. Commented May 3, 2024 at 4:22

3 Answers 3

5

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
Sign up to request clarification or add additional context in comments.

1 Comment

Despite that OP accepted this answer, it does not properly engage with the question as written. An array of characters is not a string - not in Python, not in C++ (where 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).
4

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')

3 Comments

This did the trick, although I had to convert the array to a string first using the tostring() function.
nice, happy coding :)
In Python 3, 'c' is no longer a valid type code.
2

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')

2 Comments

Any clue how to remove the byte prefixes in the array while printing it out?
@shiv Yes. You can use stdout.buffer.write. It takes bytes as input.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.