0% found this document useful (0 votes)
20 views24 pages

Python Chap 3 - Strings, List, Maps

Here are two variables pointing to my first and last name: first_name = "John" last_name = "Doe" message = "Hi there, %s %s!" print(message % (first_name, last_name))
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
20 views24 pages

Python Chap 3 - Strings, List, Maps

Here are two variables pointing to my first and last name: first_name = "John" last_name = "Doe" message = "Hi there, %s %s!" print(message % (first_name, last_name))
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

PYTHON CHAPTER 3

String, List & Maps


String
double quotes

fred = "Why do gorillas have big nostrils? Big fingers!!"


print(fred)
Why do gorillas have big nostrils? Big fingers!!

single quotes

fred = 'What is pink and fluffy? Pink fluff!!'


print(fred)
What is pink and fluffy? Pink fluff!!
To use more than one line of text in your
string (called a multiline string),
use three single quotes (‘’’),
and then hit enter between lines, like this:

fred = '''How do dinosaurs pay their bills?


With tyrannosaurus checks!'''
He said, "Aren't can't shouldn't wouldn't."
He said, "Aren't can't shouldn't wouldn't."

single_quote_str = 'He said, "Aren\'t can\'t shouldn\'t wouldn\’t.”’


print(single_quote_str)

double_quote_str = "He said, \"Aren't can't shouldn't wouldn't.\"“


>>> print(double_quote_str)
Embedding Values in Strings

If you want to display a message using the


contents of a variable,
you can embed values in a string using %s,
myscore = 1000
>>> message = 'I scored %s points'
>>> print(message % myscore)
>>> joke_text = '%s: a device for finding furniture in the dark'
>>> bodypart1 = 'Knee'
>>> bodypart2 = 'Shin’

>>> print(joke_text % bodypart1)


Knee: a device for finding furniture in the dark

>>> print(joke_text % bodypart2)


Shin: a device for finding furniture in the dark
nums = 'What did the number %s say to the number %s? Nice
belt!!'
print(nums % (0, 8))
What did the number 0 say to the number 8? Nice belt!!

nums = 'What did the number %s say to the number %s? Nice
belt!!'
print(nums % ('A','B'))
spaces = ' ' * 25
print('%s 12 Butts Wynd' % spaces)
print('%s Twinklebottom Heath' % spaces)
print('%s West Snoring' % spaces)
print()
print()
print('Dear Sir')
print()
print('I wish to report that tiles are missing
from the')
print('outside toilet roof.')
print('I think it was bad wind the other night
that blew them away.')
print()
print('Regards')
print('Malcolm Dithering')
List
>>> wizard_list = ['spider legs', 'toe of frog', 'eye of newt',
'bat wing', 'slug butter', 'snake dandruff’]

>>> print(wizard_list)
['spider legs', 'toe of frog', 'eye of newt', 'bat wing', 'slug
butter', 'snake dandruff']
Print the third item
>>> print(wizard_list[2])

Print third to fifth items


>>> print(wizard_list[2:5])
Change an item in list
>>> wizard_list[2] = 'snail tongue'
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'snake dandruff']
>>> some_numbers = [1, 2, 5, 10, 20]

>>> some_strings = ['Which', 'Witch', 'Is', 'Which’]

They might have mixtures of numbers and strings:


>>> numbers_and_strings = ['Why', 'was', 6, 'afraid', 'of', 7, 'because', 7, 8, 9]
>>> print(numbers_and_strings)
List the list
numbers = [1, 2, 3, 4]
strings = ['I', 'kicked', 'my', 'toe', 'and', 'it', 'is', 'sore']
mylist = [numbers, strings]
print(mylist)
[[1, 2, 3, 4], ['I', 'kicked', 'my', 'toe', 'and', 'it', 'is', 'sore']]
Combine two lists
list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
>>> print(list1 + list2)

list1 = [1, 2, 3, 4]
list2 = [5, 6, 7, 8]
list3 = list1 + list2
>>> print(list3)
Adding Items to a List
>>> wizard_list.append('bear burp')
>>> print(wizard_list)
['spider legs', 'toe of frog', 'snail tongue', 'bat wing', 'slug
butter', 'snake dandruff', 'bear burp']

Removing Items from a List


>>> del wizard_list[5]
>>> print(wizard_list)
Maps
In Python, a map is a collection of things, like lists and tuples. The
difference between maps and lists or tuples is that each item in a map
has a key and a corresponding value.
fav_sports = {‘Student 1' : 'Football’,
‘Student 2’ : 'Basketball’,
‘Student 3' : 'Baseball’,
‘Student 4' : 'Netball’,
‘Student 5' : 'Badminton’,
‘Student 6' : 'Rugby'}
Print value of student 4
fav_sports = {‘Student 1' : 'Football’,
print(fav_sports[‘Student 4']) ‘Student 2’ : 'Basketball’,
‘Student 3' : 'Baseball’,
‘Student 4' : 'Netball’,
‘Student 5' : 'Badminton’,
To delete a value in a map ‘Student 6' : 'Rugby'}

del fav_sports[‘Student 4’]

To replace a value in a map


fav_sports[‘Student 4'] = ‘Hockey'
Create two variables: one that points to your first
name and one that points to your last name. Now
create a string and use placeholders to print your name
with a message using those two variables,
such as “Hi there, Sasithra Sockalingam!”

You might also like