Python
strings are defined using double quotes or single quotes - it doesn't really matter but they have to match.+
operation.# Assigning a string to a variable
name = "Hello"
# Concatenating and printing strings
message = name + " world!"
print(message)
In [ ]:
# Assigning a string to a variable
# Concatenating and printing strings
It is very common to want to combine strings together into longer text, often including numbers or other values. A widely used approach to string formatting is percentage sign place holders:
%s
to insert a string%i
to insert an integer number%f
to insert a floating point number# Define a string
name = "Leighton"
# Use string formatting to place a string and an integer into another string
message = "Hello %s, your name has %i letters" % (name, len(name))
print(message)
# Define a numerical value
my_value = 3
# Represent the numeric value in two different formats
print("This is an integer: %i" % my_value)
print("This is a floating point (real) number: %f" % my_value)
(This convention was introduced in the C programming language, which was enormously influential in later programming language design, so this is seen in many different languages.)
In [ ]:
# Define a string
# Use string formatting to place a string and an integer into another string
In [ ]:
# Define a numerical value
# Represent the numeric value in two different formats
for
loop as shown later. list
of strings, a list
of integers, etc. - and you can even mix several types of data in the same list
.# Create a list of strings
names = ["Peter", "Sue", "Leighton"]
print(len(names))
# Create a list of several data types
things = ["a name", 3.5, names]
print(things)
In [ ]:
# Create a list of strings
In [ ]:
# Create a list of several data types
for
loopsPython
, have several ways to repeat a block of code multiple times.for
loop works with a loop variable (letter
in the example below) which takes in turn each of the values to be looped over (here the letters in the string
variable message
):# Create a string
message = "Hello world"
# Loop over each letter in the string and print it
for letter in message:
print(letter)
In [ ]:
# Create a string
# Loop over each letter in the string and print it
Another common situation is to loop over a list
of values:
# Loop over a list and print each element
for value in ["alpha", "beta", "gamma", "delta"]:
print(value)
In [ ]:
# Loop over a list and print each element
Elsewhere in the workshop you'll see this syntax used with other constructs.
# The Python keyword def is short for 'define'
# Here you are defining a function taking one argument
def make_message(name):
length = len(name)
# Python keyword 'return' exits the function with this value:
return "Hello %s, your name is %i characters long" % (name, length)
In [ ]:
# The Python keyword def is short for 'define'
# Here you are defining a function taking one argument
# Make a message three times
print(make_message("Peter"))
print(make_message("Sue"))
print(make_message("Leighton"))
# Assumes you've already executed the cells above which defined
# the list 'names' and the function 'make_message'
for name in names:
print(make_message(name))
In [ ]:
# Assumes you've already executed the cells above which defined
# the list 'names' and the function 'make_message'
The example below is a function which requires two arguments, and we combine it with a list
to make a rudimentary base frequency calculator:
In [ ]:
# Define a function that returns the frequency with which
# the character 'letter' occurs in the string 'text'
def letter_frequency(text, letter):
return text.count(letter) / len(text)
# This can be used as a rudimentary base frequency calculator
sequence = "AGTGACACAGGT"
for base in "ACGT":
print("Frequency of letter %s is %f" % (base, letter_frequency(sequence, base)))
In [ ]:
# Examples of some methods of a string
print(message.upper())
print(message.lower())
print(message.count("l"))
There are more Python
examples throughout these notebooks, some using more advanced things (such as with
statements). We don't have time to explain all the components of the language in detail, as we want to focus on the Bioinformatics instead, but some links are provided below as a starting point to learning other cool things about programming.