Chat room

Create a Meebo Chat Room

Saturday, December 18, 2010

Python Strings:gcufbioinfo


Let's take a look at the example code in more detail. The first thing we did was to create a string and assign it to a variable. Strings in Python are sequences of characters. You create a string literal by enclosing the characters in single ('), double (") or triple (''' or """) quotes. In the example we assigned the string literal CTGACCACTTTACGAGGTTAGC to the variable named dna.
>>> dna = 'CTGACCACTTTACGAGGTTAGC'
Then we simply typed the name of the variable, and Python responded by displaying the value of that variable, surrounding the value with quotes to remind us that the value is a string.
>>> dna 
'CTGACCACTTTACGAGGTTAGC' 
A Python string has several built-in capabilities. One of them is the ability to return a copy of itself with all lowercase letters. These capabilities are known as methods. To invoke a method of an object, use the dot syntax. That is, you type the name of the variable (which in this case is a reference to a string object) followed by the dot (.) operator, then the name of the method followed by opening and closing parentheses.
>>> dna.lower() 
'ctgaccactttacgaggttagc' 
You can access part of a string using the indexing operator s[i]. Indexing begins at zero, so s[0] returns the first character in the string, s[1] returns the second, and so on.
>>> dna[0] 
'C' 
>>> dna[1] 
'T' 
>>> dna[2] 
'G' 
>>> dna[3] 
'A' 
The final line in our screen shot shows PyCrust's autocompletion feature, whereby a list of valid methods (and properties) of an object are displayed when a dot is typed following an object variable. As you can see, Python lists have many built-in capabilities that you can experiment with in the Python shell. Now let's look at one of the other Python sequence types, the list.

0 comments:

Post a Comment

Pages 381234 »
Twitter Delicious Facebook Digg Stumbleupon Favorites More