L12 Strings Lists Sequences
L12 Strings Lists Sequences
L12 Strings Lists Sequences
H e l l o B o b
0 1 2 3 4 5 6 7 8
H e l l o B o b
0 1 2 3 4 5 6 7 8
>>> greet[0:3]
'Hel'
>>> greet[5:9]
' Bob'
>>> greet[:5]
'Hello'
>>> greet[5:]
' Bob'
>>> greet[:] This is same as greet
'Hello Bob'
L12 Strings and Lists are Sequences - 6
Int to Month
• Converting an int that stands for a month into
the three letter abbreviation for that month.
pos *= 3
monthAbbrev = months[pos:pos+3] Still not right
>>> main()
Enter a month number (1-12): 1
The month abbreviation is Jan.
>>> main()
Enter a month number (1-12): 12
The month abbreviation is Dec.
def main():
Note: Since the list is indexed starting from 0, the n-1 calculation is
straight-forward enough to put in the print statement without needing a
separate step.
L12 Strings and Lists are Sequences - 13
Lists as Sequences
def main():
print("This program converts a sequence of Unicode numbers into")
print("the string of text that it represents.\n”)
# Get the message to encode
inString = input("Please enter the Unicode-encoded message: ”)
# Loop through each substring and build Unicode message
chars = []
for numStr in inString.split():
codeNum = int(numStr) # convert digits to a number
chars.append(chr(codeNum)) # accumulate new character