Thanks to @ErykSun the solution:

Python code

string1 = "my string 1"
string2 = "my string 2"

# create byte objects from the strings
b_string1 = string1.encode('utf-8')
b_string2 = string2.encode('utf-8')

# send strings to c function
my_c_function.argtypes = [ctypes.c_char_p, ctypes.c_char_p]
my_c_function(b_string1, b_string2)
Answer from MorphieBlossom on Stack Overflow
Discussions

Convert string to char in python - Stack Overflow
Edit: The question is not about ... trying to do is to understand data types in Python and how to convert them. Convert a string to an integer is fairly easy, for example, so I was wondering if the same applies to string to char conversions.... More on stackoverflow.com
🌐 stackoverflow.com
Dealing with char*** in ctypes
I have this function in C library that I want to access from python using ctypes: static const char *names[] = {"Backplane", "ADC", "External", "Reserved"}; int test_char_ptr(const char ***names_, int *count_) { *names_ = names; *count_ = 4; return 0; } I can then do this in C app: const char ... More on discuss.python.org
🌐 discuss.python.org
0
0
December 13, 2023
C char array from python string - Stack Overflow
I have a list of strings in python which I'm trying to pass down to a C extension for character analysis. I've gotten so far as to have the list broken up into their individual string PyObjects. Next, I'm hoping to split these strings into their individual characters so that every string PyObject ... More on stackoverflow.com
🌐 stackoverflow.com
How do I convert a string to an array of chars in C?
No need. string[n] IS char[n] of string. If you try to print string[n] as a char (instead of a string) you'll get that letter. You can evaluate and modify it just like that. No fuss, no muss. (For fun: You can also printf, say, char[5] of a string as a string, and it'll print everything from that letter on.) Note that the array is zero indexed, and there's an invisible null on the end of that string that doesn't get measured. So "Hello!" will return a length of 6, but is an array of char[7]. The extra char (7) holds the null. More info: Yes, a string is just an array of char. When functions like printf are told to print a string, they go to the supplied variable's address and keep printing (or otherwise acting on) the char found there until they hit a null (\0). (This is why printing a string you've manipulated can segfault.) This is ALSO why in higher level languages that allow strings to be concatenated it's considered an "expensive" thing to do - because you can't just append to the end of an array, you need to make a new one and copy everything over. More on reddit.com
🌐 r/cs50
6
3
March 21, 2023
🌐
Python
python-list.python.narkive.com › pJK2bMLY › ctypes-delay-conversion-from-c-char-p-to-string
ctypes: delay conversion from c_char_p to string
Is there some way to tell ctypes to return an actual c_char_p, or is my best bet to return a c_void_p and cast to c_char_p when I'm reading to convert to a string? Yes, there is. When you create a subclass of c_char_p (or any other 'simple' ctypes type like c_wchar_p or even c_int and alike) then the automatic conversion to native Python types like string, unicode, integer is not done.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-convert-list-characters-string
Convert a List of Characters into a String - Python - GeeksforGeeks
If we want to join all the characters ... s = '' # Loop through each character in list 'a' and add it to string 's' for c in a: s += c print(s)...
Published   February 21, 2025
🌐
BestDivision
bestdivision.com › spread your knowledge - bestdivision › questions - bestdivision › python questions - categories - bestdivision › how to convert a c_char_p to a python string in ctypes? - bestdivision
How to convert a c_char_p to a Python string in ctypes? | BestDivision
July 15, 2023 - You can create a c_char_p variable, which can hold a C-style string. Here’s an example: To convert the c_char_p variable to a Python string, you can use the .value attribute of the c_char_p variable and then decode it to a regular string.
Find elsewhere
🌐
BestDivision
bestdivision.com › spread your knowledge - bestdivision › questions - bestdivision › python questions - categories - bestdivision › how to convert a python string to c_char_p in ctypes? - bestdivision
How to convert a Python string to c_char_p in ctypes? | BestDivision
July 15, 2023 - Learn how to convert a Python string to c_char_p using the ctypes module. This guide explains the conversion process, provides examples, and shows how to pass strings to C functions in Python effectively
🌐
Cython
cython.readthedocs.io › en › latest › src › tutorial › strings.html
Unicode and passing strings — Cython 3.3.0a0 documentation
Trying to run this code from Python (without compilation) will fail when accessing the external library. This is described in more detail in Calling C functions. ... from libc.stdlib cimport malloc from libc.string cimport strcpy, strlen cdef char* hello_world = 'hello world' cdef size_t n = strlen(hello_world) cdef char* c_call_returning_a_c_string(): cdef char* c_string = <char *> malloc( (n + 1) * sizeof(char)) if not c_string: return NULL # malloc failed strcpy(c_string, hello_world) return c_string cdef int get_a_c_string(char** c_string_ptr, Py_ssize_t *length): c_string_ptr[0] = <char *> malloc( (n + 1) * sizeof(char)) if not c_string_ptr[0]: return -1 # malloc failed strcpy(c_string_ptr[0], hello_world) length[0] = n return 0
🌐
Python.org
discuss.python.org › python help
Dealing with char*** in ctypes - Python Help - Discussions on Python.org
December 13, 2023 - I have this function in C library that I want to access from python using ctypes: static const char *names[] = {"Backplane", "ADC", "External", "Reserved"}; int test_char_ptr(const char ***names_, int *count_) { *names_ = names; *count_ = 4; return 0; } I can then do this in C app: const char **items; unsigned nr_items; result = test_char_ptr(&items, &nr_items); for (unsigned i = 0; i
🌐
Sololearn
sololearn.com › en › Discuss › 377697 › any-ideas-how-to-convert-a-char-to-string-and-vice-versa
Any ideas how to convert a char to string and vice-versa? | Sololearn: Learn to code for FREE!
You can treat it like an array and use do somethint like "char foo = mystring[2];" And if you want to add onto a string, you simply do "mystring += 'p' " 9th May 2017, 8:30 PM · aklex · Answer · Learn more efficiently, for free: Introduction ...
🌐
W3Schools
w3schools.com › c › c_strings.php
C Strings
Since strings are actually arrays in C, you can access a string by referring to its index number inside square brackets []. This example prints the first character (0) in greetings:
🌐
WsCube Tech
wscubetech.com › resources › python › programs › string-to-char
Convert String to Char in Python (6 Programs)
November 3, 2025 - Learn how to convert string to char in Python with 6 simple programs. Explore step-by-step examples, outputs, and clear explanations. Read now!
🌐
GeeksforGeeks
geeksforgeeks.org › python › c-strings-conversion-to-python
C strings conversion to Python - GeeksforGeeks
April 2, 2019 - PyObject *obj = Py_BuildValue("s#", s, len); If s is encoded in some other known encoding, a string using PyUnicode_Decode() can be made as: ... PyObject *obj = PyUnicode_Decode(s, len, "encoding", "errors"); // Example obj = PyUnicode_Decode(s, len, "latin-1", "strict"); obj = PyUnicode_Decode(s, len, "ascii", "ignore"); If a wide string needs to be represented as wchar_t *, len pair. Then are few options as shown below - ... // Wide character string wchar_t *w; // Length int len; // Option 1 - use Py_BuildValue() PyObject *obj = Py_BuildValue("u#", w, len); // Option 2 - use PyUnicode_FromWideChar() PyObject *obj = PyUnicode_FromWideChar(w, len);
Top answer
1 of 1
4

You can use PyUnicode_AsEncodedString, which

Encode a Unicode object and return the result as Python bytes object. encoding and errors have the same meaning as the parameters of the same name in the Unicode encode() method. The codec to be used is looked up using the Python codec registry. Return NULL if an exception was raised by the codec.

see https://docs.python.org/3/c-api/unicode.html#c.PyUnicode_AsEncodedString

Then with PyBytes_AsString you get a pointer to internal buffer with a terminating NUL byte. This buffer must neither be deallocated nor modified. If you need a copy you could use e.g. strdup.

see https://docs.python.org/3/c-api/bytes.html#c.PyBytes_AsString

Slightly modifying your code it could look like this:

PyObject *encodedString = PyUnicode_AsEncodedString(commandString, "UTF-8", "strict");
if (encodedString) { //returns NULL if an exception was raised
    char *commandChars = PyBytes_AsString(encodedString); //pointer refers to the internal buffer of encodedString
    if(commandChars) {
        printf("the string '%s' consists of the following chars:\n", commandChars);
        for (int i = 0; commandChars[i] != '\0'; i++) {
            printf("%c ", commandChars[i]);
        }
        printf("\n");
    }
    Py_DECREF(encodedString);
}

If one would test with:

import cExt

fruits = ["apple", "pears", "cherry", "pear", "blueberry", "strawberry"]         
res = cExt.listCheck(fruits)
print(res)

The output would be:

the string 'apple' consists of the following chars:
a p p l e 
the string 'pears' consists of the following chars:
p e a r s 
the string 'cherry' consists of the following chars:
c h e r r y 
the string 'pear' consists of the following chars:
p e a r 
the string 'blueberry' consists of the following chars:
b l u e b e r r y 
the string 'strawberry' consists of the following chars:
s t r a w b e r r y 
[1, 1, 1, 1, 1, 1]

Side note not directly related to the question: Your CitemCheck function returns a pointer to int, but if looking at how it is called, it seems that you want to return an int value. The function signature should look more like this:

static int CitemCheck(PyObject *commandString, int commandStringLength)

(note the removed * after int).

🌐
docs.python.org
docs.python.org › 3 › c-api › string.html
Python/C API reference manual — Python 3.14.3 documentation
This manual documents the API used by C and C++ programmers who want to write extension modules or embed Python. It is a companion to Extending and Embedding the Python Interpreter, which describes...
🌐
Reddit
reddit.com › r/cs50 › how do i convert a string to an array of chars in c?
r/cs50 on Reddit: How do I convert a string to an array of chars in C?
March 21, 2023 -

I know that a string is already technically an array of chars, but when I try to use toupper(string), it doesn’t work because toupper is designed to capitalize chars and not strings, per the documentation. I’ve been making it overly complicated and it’s stressing me out. So to start, I created an “int N=strlen(string);”, then created an array that’s “char upper[N];”. Then I write a for loop written as(please forgive the terrible syntax I’m about to write), “for (int i = 0; i < N; i++) { toupper(upper[j]); }”. What am I doing wrong?

🌐
Quora
quora.com › How-do-I-convert-a-char-to-string-in-C
How to convert a char to string in C - Quora
Answer (1 of 6): C is a procedure oriented language. Unlike C++ it does not have any string object support or java that has string as a primitive data type. In C string is simply an array of characters but the only condition is that it must be terminated by a null character,i.e '\0’. To convert...
🌐
Studytonight
studytonight.com › python-howtos › how-to-convert-string-to-character-array-in-python
How to Convert String to Character Array in Python - Studytonight
January 30, 2021 - This article shows conversion of string to character array using loop, typecasting and extend() function.