1

i want to create a string S , which can be used as an array , as in each element can be used separately by accesing them as an array.

1
  • 1
    You should add more details. Python strings are already working like lists. What exactly do you want? Provide an example, a context, a problem, so that one understands what you're after. Commented Apr 10, 2010 at 7:32

3 Answers 3

5

That's how Python strings already work:

>>> a = "abcd"
>>> a[0]
'a'
>>> a[2]
'c'

But keep in mind that this is read only access.

Sign up to request clarification or add additional context in comments.

Comments

3

You can convert a string to a list of characters by using list, and to go the other way use join:

>>> s = 'Hello, world!'
>>> l = list(s)
>>> l[7] = 'f'
>>> ''.join(l)
'Hello, forld!'

5 Comments

but then i always get list indexing errors . i would like to define the string as in c++ , where String S , gives me a string S which can be used as an array of characters.
@mekasperasky: you can't modify the original string, you can only modify the list then convert it back to a string. Strings are immutable in Python. Modifying one character in a string can be an expensive operation as you have to copy the entire string.
If you are getting index errors then you are probably trying to access characters off the end of the string - you will get the same error whether you are using a string or a list. This is a bug in your code, not a problem with Python's strings or lists. Show us the code!
pastebin.com/ptfQHina thats the code .. can you try debugging it . its an rc 5 algorithm ..
@mekasperasky: A lot of that code seems unnecessary. At the start you assign to pt1, and then call append. Then before you've used the resulting value, you assign to it again - destroying the old value. And then on the next line you assign to it a third time. This has to be wrong.
0

I am a bit surprised that no one seems to have written a popular "MutableString" wrapper class for Python. I would think that you'd want to have it store the string as a list, returning it via ''.join() and implement a suite of methods including those for strings (startswith, endswith, isalpha and all its ilk and so one) and those for lists.

For simple operations just operating on the list and using ''.join() as necessary is fine. However, for something something like: 'foobar'.replace('oba', 'amca') when you're working with a list representation gets to be ugly. (that=list(''.join(that).replace(something, it)) ... or something like that). The constant marshaling between list and string representations is visually distracting.

2 Comments

There is UserString.MutableString in the standard library, but it's slow and deprecated since 2.6 (removed in 3.0).
Apparently the bytearray (mutable sequence of bytes) was added back in 2.6: docs.python.org/2/library/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.