1. How to Program, Part I
  2. How to Program, Part II
  3. How to Program, Part III
  4. How to Program, Part IV
  5. How to Program, Part V
  6. How to Program, Part VI
  7. exercises
  8. pyMPI tutorial
  9. Calculating PI, Part I
  10. Calculating PI, Part II
  11. Calculating PI, Part III
  12. Dividing Work
  13. More MPI
  14. Poogle - Web Search
  15. Mandelbrot Sets
  16. Mandelbrot, The Code
  17. Mandelbrot, The Images
  18. Mandelbrot In CUDA
  19. Conway's Life, Part I
  20. Life Code Listing
  21. Conway's Life, Part II
  22. MPI Life Code Listing

How to Program, Part V

It is easy to take strings apart and put them back together using Python.

1x = "Don't break up with me!"
2start = x[0:5] # selects characters 0 to 4
3end   = x[5:]  # selects characters 5 until the end
4print "Do",end
5end += str(3) # add a number onto a string
6print "and again... Do",end
$ python ./pieces.py
Do  break up with me!
and again... Do  break up with me!3

We can also find a substring in a larger string.

1x = "Where am I?"
2n = x.find("am")
3print "n=",n
4print x[0:n],"|am|",x[n+2:]
$ python ./where.py
n= 6
Where  |am|  I?

This means we can even replace a substring if we want to:

1x = "Where am I?"
2n = x.find("am")
3xnew = x[0:n]+"are"+x[n+2:]
4print xnew
$ python ./where2.py
Where are I?

Notice that we can use "+" to put two strings together.

We could capture this behavior using a function:

1def repl(needle,new_needle,haystack):
2    n = haystack.find(needle)
3    return haystack[0:n]+new_needle+haystack[n+len(needle):]
5print repl("am","are","Where am I?")
$ python ./repl.py
Where are I?

This all works very well until we look for something that isn't there.

1import repl
3print repl.repl("am","are","What's going on here?")
$ python ./where3.py
Where are I?
What's going on herearehat's going on here?

What happened? To figure this out, we will make a small program:

1s = "nice day"
2n = s.find("night")
3print n
4print s[0:n]
$ python ./uhoh.py
nice da

First we see that find() returns a -1 if it fails to find something. Then we discover that the range operation interprets negative numbers in a funny way. It treats s[0:-1] the same as s[0:len(s)-1].

To avoid this problem, we make a new version of repl.

1def repl(needle,new_needle,haystack):
2    n = haystack.find(needle)
3    if n < 0:
4        return haystack
5    else:
6        return haystack[0:n]+new_needle+haystack[n+len(needle):]
8print repl("am","are","What's going on here?")
$ python ./repl2.py
What's going on here?

Now our program behaves as expected. If our needle is not present in the haystack, then we don't make any substitutions.

Of course, it should not surprise you to learn that Python already has a built in method for replacing strings.

1print "Where am I?".replace("am","are")
$ python ./doh.py
Where are I?

Now, however, you know enough about Python and programming to be able to write this function, and perhaps similar ones, yourself.

Here are a few other useful string methods:

1print "Hello".upper()
2print "Hello".lower()
3print "aa.bbbb.c".split(".")
$ python ./ostring.py
['aa', 'bbbb', 'c']


  1. Write a function replaces a word with an upper case version of itself, i.e. if we call repl("foo","there be foo here") we get "there be FOO here".
  2. Write a function that replaces all occurrences of a given needle with a new needle, i.e. replAll("and","&","and a one and a two and a three") produces "& a one & a two & a three".