Strings
Strings
Strings
• A string is a sequence of letters (called characters).
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.
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
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
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
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