我正在进行从 C 到 python 的代码转换... 我有一个 char 数组,用作字符串缓冲区
char str[25]; int i=0;
str[i]='\0';
这里我将采用不同的值,甚至 str[i] 也持有不同的值
我想要Python中的等效代码...就像一个字符串缓冲区,我可以在其中存储n个编辑字符串内容。我什至尝试使用列表,但它不是很有效,所以还有其他出路吗? Python中有字符串缓冲区吗?如果是的话我该如何根据这些使用它?
最佳答案
在 Python 中使用 bytearray
存储可变的字节数据列表:
s = bytearray(b'My string')
print(s)
s[3] = ord('f') # bytes are data not characters, so get byte value
print(s)
print(s.decode('ascii')) # To display as string
输出:
bytearray(b'My string')
bytearray(b'My ftring')
My ftring
如果您需要改变 Unicode 字符串数据,那么 list
就是正确的选择:
s = list('My string')
s[3] = 'f'
print(''.join(s))
输出:
My ftring
关于C 到 python 代码转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19638305/