ch03-CreatingModifyingText
ch03-CreatingModifyingText
Modifying Text
2)Variable a is still 10
3)Variable b is now 10
1)File name
2)Picture
3)String
4)Float
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Triple Quotes allow us to Embed
>>> sillyString()
This is using triple quotes. Why?
Notice the different lines.
And we can't ignore the use of apostrophes.
1)13
2)An error
3)“12”
4)“121”
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
How could this be?
2)string is “1.0000000000/3”
3)string is “0.333333333333”
4)©string is “1/3.0000000000”
2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Using string concatenation to
tell a MadLib story
t
h
r
!
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Two different ways to deal with
case
>>> double("rubber
duck")
rruubbbbeerr dduucckk
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
More interesting: Reverse
>>> reverse("rubber
duck")
kcud rebbur
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Mirroring
We can add the index variable to both sides of
the pile
def exclaim1(somestring):
target = "!"
for char in somestring:
target = target + char def exclaim2(somestring):
print target target = "!"
for char in somestring:
target = char + target
print target
def exclaim3(somestring):
target = ""
for char in somestring:
target = target + char + "!"
print target © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
One of these, when you call it with the
input of “Between” will print:
>B<e<t<w<e<e<n<
Which one?
def between1(somestring):
target = ">"
for char in somestring: def between2(somestring):
target = target + char target = "<>"
print target+"<" for char in somestring:
target = char + target + char
print target
def between3(somestring):
target = ">"
for char in somestring:
target = target + char + "<"
print target © 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Creating language patterns
def doubledutch(name):
pile = ""
for letter in name:
if letter.lower() in "aeiou":
pile = pile + letter
if not (letter.lower() in "aeiou"):
pile = pile + letter + "u" + letter
print pile
>>> doubledutch("mark")
mumarurkuk
>>> doubledutch("bill")
bubilullul
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Using square bracket notation
>>> phrase = "Hello world!"
>>> phrase[0]
'H'
>>> phrase[1] The first character
'e' is at index 0.
>>> phrase[2]
Negative index
'l'
>>> phrase[6] values reference
'w’ the end of the list
>>> phrase[-1]
'!'
>>> phrase[-2]
'd'
2)string is “1.0000000000/3”
3)string is “0.333333333333”
4)string is ©“1/3.0000000000”
2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Use the range() function to
generate index values
>>> print range(0,5)
>>> for index in
[0, 1, 2, 3, 4]
range(0,3):
>>> print range(0,3)
... print index
[0, 1, 2]
...
>>> print range(3,0)
0
[]
1
>>> print range(0,5,2)
2
[0, 2, 4]
>>> print range(0,7,3)
[0, 3, 6]
>>> print range(5)
[0, 1, 2, 3, 4]
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
Print the string, by index
def parts2(string):
for index in
range(len(string)):
print string[index]
>>> parts2("bear")
b
e
a
r
1)print word[4]
2)print word[3]
3)print word[5]
4)print word[e]
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
If I want to print the “g”
which should I use?
1)print word[-1]
2)print word[‘g’]
3)print word[9]
4)print word[8]
© 2016 Pearson Education, Inc., Hoboken, NJ. All rights reserved.
If I want to print “thing”
which should I use?
1)print word[4:9]
2)print word[5:9]
3)print word[4:10]
4)print word[4:8]