Round 2.1 --- Strings (in Python)

Oct 30, 2012 • Jason Pell

What is a string?

A string is a datatype in Python used for storing a sequence of characters (e.g. ‘hello world’ or ‘e equals 2.71…’ or ‘50% of 20% is 10%’) where a character can be a letter, number, or a variety of other symbols including a space or new line character.

my_str = ‘the quick fox jumps over the lazy dog’
print my_str
the quick fox jumps over the lazy dog

Above, we created a variable called my_str that contains the string, “the quick fox jumps over the lazy dog.” The single quote is used to denote the beginning and end of the string.

Indexing

It is possible to access an character/element in a string with the notation my_str[i] where my_str is the variable name of the string and i is the index of the element. The index is a number from 0 to one less than the length of the string where the first element is accessed with 0, the 2nd element with 1, 3rd element with 2, and so on (so don’t forget to start counting at 0!) See the example below:

the_str = “software carpentry”
print the_str[3]
t

In the first line, we created a variable called the_str that contains the string: software carpentry. We print out the 4th element of the_str by using the index value 3, and then in the last line we see that the character t is successfully printed.

Slicing and Substrings

You can also grab a substring, or an ordered part of a string, by doing string slicing.

abc = “the value of pi is 3.141592…”
print abc[13:15]
pi
some_slice = abc[19:]
print some_slice
3.141592…

In the top line, we created a string called abc. Next, we used string slicing to print the characters in the string abc from index 13 up to (but not including!) 15, which happens to be the substring “pi.” There are other variations of string slicing, one example of which is to grab a substring at the end of a string (i.e. a suffix) beginning at a given index. In the example above, we obtain the substring beginning at index 19 up to the end of the string and store the result in some_slice. Finally, we print out our result (3.141592…).

Caveat about Numbers

Note that a number can be a string rather than an integer, which means that in those cases you cannot do any numeric processing with them.

num_one = ‘100’
num_two = ‘200’
print num_one + num_two
100200

In the first two lines above, we created variables num_one and num_two that hold the strings 100 and 200, respectively. However, rather than add the numbers together, the result was a concatenation.

Fill in the Blank

a = “the quick fox jumps over the lazy dog”
print a
the quick fox jumps over the lazy dog
some_char = _[12] # fill the blank to access element 12 in variable a
print some_char
_
# fill in what is printed
j_char = a[_] # fill the index for the letter j
some_word = a[4:9]
print some_word
_
__ # fill in what is printed
print a[_\_:__] # fill in the indices to print the word jumps
jumps

Usefulness

Strings are heavily used in Python to store any kind of textual data. For example, one could open a text file and read each line in the file. The lines would likely be best stored in the script as strings, and then each line can be manipulated or parsed from there. Strings are an integral data type in Python, and it is important to become proficient in indexing and slicing along with other Python string features to become a proficient Python programmer.

Time

It took me about 2 hours to decide on a topic for a concept map, make the map, and then write up this report.