Department of Mathematics, Computer Science, and
                Statistics
St.
                Lawrence University
Zonohedron
 

What is this object?

...

Links
Overview
Schedule
Grading Info
Class Notes

 

CS 140: Introduction to Computer Programming


Manipulating Strings

As we have seen, a string is a sequence of characters, including letters, numbers, symbols, and even spaces, usually typed on a keyboard.  We specify strings by placing them within quotes, such as "Big Bang Theory", "90210", or "#&@$(%!".   For the most part our strings will involve only letters, numbers and spaces.

We can assign a string to a variable name, just as we can for numbers.  We can also print strings to the screen.  Therefore the code

place = "carnival"
print "Let's go to the",
print place,
print "!"


would yield the output  Let's go to the carnival !

We can "add" two or more strings with a + sign.  This is known as concatenation and has the effect of creating one long string consisting of the smaller strings placed immediately next to one another.  Thus defining word1="moun" and word2="tain" would result in word3=word1+word2+"s" being "mountains".

Concatenation can be used to fine tune output format since it does not introduce an extra space, unlike the comma.  For instance, notice the (unwanted) space between the word carnival and the exclamation point above.  If we had instead typed

place = "carnival"
print "Let's go to the", place+"!"


then the output would read   Let's go to the carnival!  as intended.

Python also understands how to "multiply" a string by a positive integer; it simply produces that many copies of the string.  Thus "la"*5 would result in "lalalalala".  This construction provides a shortcut for tabbing over repeatedly or inserting lots of carriage returns.  Thus to skip seven lines and tab over six times, type

print "\n"*7, "\t"*6,

Similarly, one can easily print a precise amount of white space.  Thus  print " "*9 gives exactly nine spaces.

Python will tell you how long a particular string is via the len function.  For example, len("carnival") gives 8, but len(" carnival ") is 10, because spaces count as characters.  Along the same lines, the program

word = raw_input("Enter the name of a country: ")
print "Your country has", len(word), "letters."


will count letters in a word entered by the user.

Finally, Python will allow you to pinpoint a single character or row of characters within a string once you indicate the position (or index) of the desired characters.  The catch is that you must count positions like a computer, which means starting with 0.  Thus the string "carnival" has a 'c' in position 0, an 'a' in position 1, all the way up to an 'l' in position 7.

To pick off a single character within a string, put its position in square brackets following the string.  Thus word[0] will give the first letter of the word, while word[3] is the fourth letter.  One can also type word[-1] to obtain the last letter, or word[-2] for the next to last letter, and so forth.

On a slightly fancier note, you can obtain an entire range of characters by specifying a pair of positions, such as in word[2:5]BEWARE: this only returns the letters in positions 2, 3, 4; not the letter in position 5!  (So this range behaves a lot like the range in for loops.)  So if  word="carnival" then word[2:5] is equal to "rni".

Let's illustrate all of these ideas by creating a SLU username:

first = raw_input("Enter your first name: ")
middle = raw_input("Enter your middle name: ")
last = raw_input("Enter your last name: ")
year = raw_input("Enter your starting year at SLU: ")
username = first[0]+middle[0]+last[0:4]+year[2:4]
print "Your username is", username+"."


So if Fred Ivan Shingledecker started at SLU in 2012, the above program would assign him the username fishin12.