# How to Program, Part V

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

 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 5 end += str(3) # add a number onto a string 6 print "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.

 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:

 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:

 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.

 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:

 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.

 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.

 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:

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

Problems:

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".
 prev next