Puneeth P
Puneeth P

Reputation: 155

C to python code conversion

I'm doing code conversion form C to python... I have a char array and is used as a String buffer

 char str[25]; int i=0;

 str[i]='\0';

Here i will be taking different values and even str[i] also holds different values

I want the equivalent code in python... Like a string buffer, where i can store n edit string contents inside. I even tried using a list but it is not very efficient so is there any other way out?? Is there any string buffer thing in Python? If so how can i use it according to these?

Upvotes: 0

Views: 215

Answers (1)

Mark Tolonen
Mark Tolonen

Reputation: 178115

Use a bytearray to store a mutable list of byte data in Python:

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

Output:

bytearray(b'My string')
bytearray(b'My ftring')
My ftring

If you need to mutate Unicode string data, then list is the way to go:

s = list('My string')
s[3] = 'f'
print(''.join(s))

Output:

My ftring

Upvotes: 1

Related Questions