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. exercises
  7. pyMPI tutorial
  8. Calculating PI, Part I
  9. Calculating PI, Part II
  10. Calculating PI, Part III
  11. Poogle - Web Search
  12. Mandelbrot Sets
  13. Mandelbrot, The Code
  14. Mandelbrot, The Images
  15. Conway's Life, Part I
  16. Life Code Listing
  17. Conway's Life, Part II
  18. MPI Life Code Listing

How to Program, Part V

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

file: pieces.py
1 x = "Don't break up with me!"
2 start = x[0:5] # selects characters 0 to 4
3 end   = x[5:]  # selects characters 5 until the end
4 print "Do",end
> python pieces.py
Do  break up with me!

We can also find a substring in a larger string.

file: where.py
1 x = "Where am I?"
2 n = x.find("am")
3 print "n=",n
4 print 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:

file: where2.py
1 x = "Where am I?"
2 n = x.find("am")
3 xnew = x[0:n]+"are"+x[n+2:]
4 print 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:

file: repl.py
1 def repl(needle,new_needle,haystack):
2     n = haystack.find(needle)
3     return haystack[0:n]+new_needle+haystack[n+len(needle):]
4 
5 print 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.

file: where3.py
1 import repl
2 
3 print 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:

file: uhoh.py
1 s = "nice day"
2 n = s.find("night")
3 print n
4 print s[0:n]
> python uhoh.py
-1
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.

file: repl2.py
1 def 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):]
7 
8 print 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.

file: doh.py
1 print "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:

file: ostring.py
1 print "Hello".upper()
2 print "Hello".lower()
3 print "aa.bbbb.c".split(".")
> python ostring.py
HELLO
hello
['aa', 'bbbb', 'c']