Strings
Strings
• A string is a sequence of letters (called characters).
• In Python, strings start and end with single or double
quotes.
>>> “foo”
‘foo’
>>> ‘foo’
‘foo’
Defining strings
• Each string is stored in the computer’s
memory as a list of characters.
>>> myString = “GATTACA”
myString
Multiline String
• can assign a multiline string to a variable by using either three double
quotes or three single quotes:
a = “ “ “Hi,
Hello how are you,
Welcome to Mech dept
We are moving towards innovation“ “ "
print(a)
OUTPUT:
Hi,
Hello how are you,
Welcome to Mech dept
We are moving towards innovation
Accessing single characters
• You can access individual characters by using indices in square brackets.
>>> myString = “GATTACA”
>>> myString[0]
‘G’
>>> myString[1]
‘A’
>>> myString[-1]
‘A’ Negative indices start at the end
>>> myString[-2] of the string and move left.
‘C’
>>> myString[7]
Traceback (most recent call last):
File "<stdin>", line 1, in ?
IndexError: string index out of range
Accessing substrings
>>> myString = “GATTACA”
>>> myString[1:3]
‘AT’
>>> myString[:3]
‘GAT’
>>> myString[4:]
‘ACA’
>>> myString[3:5]
‘TA’
>>> myString[:]
‘GATTACA’
Special characters
• The backslash is used to
introduce a special character.
Escape Meaning
sequence
>>> "He said, "Wow!"" \\ Backslash
File "<stdin>", line 1
"He said, "Wow!""
^
\’ Single quote
SyntaxError: invalid
syntax \” Double
>>> "He said, 'Wow!'" quote
"He said, 'Wow!'"
>>> "He said, \"Wow!\"" \n Newline
'He said, "Wow!"'
\t Tab
M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• We can also look at any
continuous section of a
string using a colon >>> s = 'Monty Python'
operator >>> print s[0:4]
• The second number is Mont
one beyond the end of the >>> print s[6:7]
slice - “up to but not P
including” >>> print s[6:20]
Python
• If the second number is
beyond the end of the
string, it stops at the end
Slicing Strings
M o n t y P y t h o n
0 1 2 3 4 5 6 7 8 9 10 11
• If we leave off the
first number or the
last number of the >>> s = 'Monty Python'
>>> print s[:2]
slice, it is Mo
>>> print s[8:]
assumed to be the Thon
beginning or end >>> print s[:]
Monty Python
of the string
respectively
Slicing Strings
More string functionality
>>> len(“GATTACA”) ← Length
7
>>> “GAT” + “TACA”
← Concatenation
‘GATTACA’
>>> “A” * 10
‘AAAAAAAAAA ← Repeat
>>> “GAT” in “GATTACA”
True ← Substring test
>>> “AGT” in “GATTACA”
False
>>> “GAT” + “ “ +“TACA” ← Concatenation
‘GAT TACA’
String methods
• In Python, a method is a function that is defined with
respect to a particular object.
• The syntax is <object>.<method>(<parameters>)
>>> dna = “ACGT”
>>> dna.find(“T”)
3
>>>fruit = 'banana'
>>> pos = fruit.find('na')
>>> print pos
2
String methods
>>> "GATTACA".find("ATT")
1
>>> "GATTACA".count("T")
2
>>> "GATTACA".lower()
'gattaca'
>>> "gattaca".upper()
'GATTACA'
>>> "GATTACA".replace("G", "U")
'UATTACA‘
>>> "GATTACA".replace("C", "U")
'GATTAUA'
>>> "GATTACA".replace("AT", "**")
'G**TACA'
>>> "GATTACA".startswith("G")
True
>>> "GATTACA".startswith("g")
False
Split methods
• Split a string into a list where each word is a
list item:
• Syntax:
string.split(separator, maxsplit)
• Example:
• txt = "welcome to the jungle"
x = txt.split()
print(x)
• OUTPUT
– ['welcome', 'to', 'the', 'jungle']
Split cntd..
2. txt = "hello my name is Peter I am 26 years old"
x = txt.split(", ")
print(x)
OUTPUT:
['hello', 'my name is Peter', 'I am 26 years old']
3.txt = "apple#banana#cherry#orange"
x = txt.split("#")
print(x)
OUTPUT
['apple', 'banana', 'cherry', 'orange']
4.txt = "apple#banana#cherry#orange"
x = txt.split("#", 1)
print(x)
OUTPUT
['apple', 'banana#cherry#orange']
String Library
str.capitalize() str.replace(old, new[, count]
str.center(width[, fillchar]) str.lower()
str.endswith(suffix[, start[, end]])
str.rstrip([chars])
str.find(sub[, start[, end]])
str.lstrip([chars]) str.strip([chars])
str.upper()
http://docs.python.org/lib/string-methods.html
Example-len
• Using a while fruit = 'banana'
statement and an index = 0
iteration variable, and while index < len(fruit) :
the len function, we
can construct a loop letter = fruit[index]
to look at each of the print index, letter
letters in a string index = index + 1
individually
OUTPUT
0b
1a
2n
3a
4n
5a
Looping and counting
• This is a simple loop word = 'banana'
that loops through count = 0
each letter in a string for letter in word :
and counts the
number of times the if letter == 'a' :
loop encounters the count = count + 1
'a' character. print count
OUTPUT:
3
Strings are immutable
• Strings cannot be modified; instead, create a
new one.
>>> s = "GATTACA"
>>> s[3] = "C"
Traceback (most recent call last):
File "<stdin>", line 1, in ?
TypeError: object doesn't support item assignment
>>> s = s[:3] + "C" + s[4:]
>>> s
'GATCACA'
>>> s = s.replace("G","U")
>>> s
'UATCACA'
Strings are immutable
• String methods do not modify the string;
they return a new string.
>>> sequence = “ACGT”
>>> sequence.replace(“A”, “G”)
‘GCGT’
>>> print sequence
ACGT
>>> sequence = “ACGT”
>>> new_sequence = sequence.replace(“A”, “G”)
>>> print new_sequence
GCGT
String summary
Basic string operations:
S = "AATTGG" # assignment - or use single quotes ' '
s1 + s2 # concatenate
s2 * 3 # repeat string
s2[i] # index character at position 'i'
s2[x:y] # index a substring
len(S) # get length of string
int(S) # or use float(S) # turn a string into an integer or floating
point decimal
Methods:
S.upper()
S.lower()
S.count(substring)
S.replace(old,new)
S.find(substring)
S.startswith(substring), S. endswith(substring)
Printing:
print(var1,var2,var3) # print multiple variables
print("text",var1,"text“) # print a combination of explicit text
(strings) and variables
Questions
• b = "Hello, World!"
print(b[-5:-2])
• a = " Hello, World! "
print(a.strip())
• a = "Hello, World!"
print(a.split(","))
• txt = "The rain in Spain stays mainly in the plain"
x = "ain" not in txt
print(x)
Questions
• age = 36
txt = "My name is John, and I am { }"
print(txt.format(age))
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want { } pieces of item { } for { } dollars."
print(myorder.format(quantity, itemno, price))
• quantity = 3
itemno = 567
price = 49.95
myorder = "I want to pay {2} dollars for {0} pieces of item
{1}."
print(myorder.format(quantity, itemno, price))
Escape Sequence Questions
• txt = "Hello\nWorld!"
print(txt)
• txt = "Hello \bWorld!"
print(txt)
• txt = 'It\'s alright.‘
print(txt)
• fruit = 'banana'
aa = fruit.find('z')
print aa