How do I splice a python string programmatically?

Posted by Robin Welch on Stack Overflow See other posts from Stack Overflow or by Robin Welch
Published on 2010-05-27T11:14:45Z Indexed on 2010/05/27 11:21 UTC
Read the original article Hit count: 233

Very simple question, hopefully. So, in Python you can split up strings using indices as follows:

>>> a="abcdefg"
>>> print a[2:4]
cd

but how do you do this if the indices are based on variables? E.g.

>>> j=2
>>> h=4
>>> print a[j,h]
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: string indices must be integers

© Stack Overflow or respective owner

Related posts about python

Related posts about beginner