1. +
Dr. Ramadan Babers
Faculty of Science - Helwan University
Spring – 2019
Python
LEC - 03
for
Biologists
https://www.linkedin.com/in/ramadan-babers-phd-78976345
3. 3
Storing strings in variables
We can take a string and assign a name to it
using an equals sign – we call this a variable:
We can use the variable name instead of the
string itself – for example, we can use it in a print
statement
4. 4
Storing strings in variables
variable names are arbitrary – that means that
we can pick whatever we like to be the name of
a variable.
variable names are case-sensitive,so
my_dna,
MY_DNA,
My_DNA and
My_Dna
are all separate variables.
5. 5Tools for manipulating strings
1- Concatenation
We can concatenate (stick together) two strings
using the + symbol1 .This symbol will join
together the string on the left with the string on
the right:
6. 6Tools for manipulating strings
1- Concatenation (cont.)
use variables that point to strings:
join multiple strings together in one go:
concatenation inside a print statement:
7. 7Tools for manipulating strings
2- Finding the length of a string
useful built-in tool in Python is the len function (len is short for length).
No output appears
Why ??????
we need to store it in a variable
8. 8Tools for manipulating strings
2- Finding the length of a string
Python treats strings and numbers differently.
len function: the result (or return value) is not a string, it's a number.
calculates the length of a DNA sequence
and then prints a message telling us the length:
my_dna = "ATGCGAGT"
dna_length = len(my_dna)
print("The length of the DNA sequence is " + dna_length)
9. 9Tools for manipulating strings
2- Finding the length of a string
Python has a built-in solution – a function called str which
turns a numberinto a string so that we can print it.
10. 10Tools for manipulating strings
3- Changing case
A method is like a function, but instead of being built in to the Python language,
it belongs to a particular type.
Method & Function
Ex. lower and upper are belong to the string type.
11. 11Tools for manipulating strings
3- Changing case (Quiz)
Upper case: ATGC
Lower case: atgc
Write a python code that print the sequence of DNA “ATGC” in
lower and upper case. The result will as shown below.
12. 12Tools for manipulating strings
4- Replacement
it takes two arguments (both strings) and returns a copy of the variable
where all occurrences of the first string are replaced by the second
string.
replace method
replace("v", "y")
replace("vls", "ymt")
the original variable
13. 13Tools for manipulating strings
5- Extracting part of a string
If we want a short portion of a long string, this is known as taking a substring. To get
it, we follow the variable name with a pair of square brackets which enclose a start
and stop position, separated by a colon (:).
protein = "vlspadktnv"
print(protein[3:5])
print(protein[0:6])
print(protein[0:60])
14. 14Tools for manipulating strings
6- Counting and finding substrings
A very common job in biology is to count the number
of times some pattern occurs in a DNA or protein
sequence.
counting the number of times a
substring occurs in a string.
method that does the job is
called count
16. 16Tools for manipulating strings
6- Counting and finding substrings
find method takes a single string argument, just like count, and
returns a number which is the position at which that substring first
appears in the string
the index of the substring