having fun with code

Little tricks: editing strings by index in Javascript and Python

Editing strings by index maybe something that we don’t do all the time. But it’s one of these things that, coming from languages like C, one would assume is as trivial as assigning the value of an indexed position. Something like this:

  1. var a = "hello world"
  2. a[0] = "H"
  3. console.log(a) // outputs "hello world"

Go ahead and paste that code in your favorite javascript console. You would expect the output to be “Hello there”, but it is not.
Same goes with Python, check this code:

  1. >>> a = "hello world"
  2. >>> a[0] = "H"
  3. Traceback (most recent call last):
  4.   File "<stdin>", line 1, in <module>
  5. TypeError: 'str' object does not support item assignment
  6. >>> a[0] = 'H'
  7. Traceback (most recent call last):
  8.   File "<stdin>", line 1, in <module>
  9. TypeError: 'str' object does not support item assignment

This is because strings are read only and cannot be modified at all. Only replaced with new strings.

The solution: convert to array and back to string

There may be other solutions out there, I’m sure, but this is the solution I use: Both in Python and Javascript, a string can be converted into an array or list of characters (actually, list of strings). Then we can modify values by index and finally join the output into a new string.

Javascript:

  1. var a = "hello world"
  2. a = a.split("")
  3. a[0] = "H"
  4. a = a.join("")
  5. console.log(a) // outputs "Hello world"

Python:

  1. >>> a = "hello world"
  2. >>> a = list(a)
  3. >>> a[0] = "H"
  4. >>> a = "".join(a)
  5. >>> a
  6. 'Hello world'

It’s interesting to see how the syntax for joining arrays into strings is totally the opposite in Javascript and Python. In Javascript, join() is a function of the Array object while in Python join() is a function of the string class.

In the other hand, split() is a method of the String object in Javascript. There is a split() function on the string class in Python, but it cannot be used to split strings without a specific separator. The list() function/constructor does the trick.

Related Posts:

1 Tweet

1 Comment to Little tricks: editing strings by index in Javascript and Python

  1. July 1, 2010 at 09:46 | Permalink

    New post: edit strings by index: http://dev.enekoalonso.com/2010/07/01/little-tricks-editing-strings-by-index-in-javascript-and-python

    This comment was originally posted on Twitter

Leave a Reply

You can use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Additional comments powered by BackType

About the blog

This is a blog about development, focused mainly on Javascript but also other languages like python, shell scripts and more.

About the author

Eneko Alonso is a software engineer and UI developer with more than eight years of experience in software and web development. He lives in San Luis Obispo, California and works at LEVEL Studios.

Contact Info

Contact Info