7.3 연습 문제

7.1

mystery = '\U0001f4a9'
print(mystery)
import unicodedata
unicodedata.name(mystery)

💩 ‘PILE OF POO’

7.2

pop_bytes = mystery.encode('utf-8')
print(pop_bytes)

b’\xf0\x9f\x92\xa9’

7.3

pop_string = pop_bytes.decode('utf-8')
print(pop_string)

💩

7.4

'My kitty car likes %s' % 'roast beef'

‘My kitty car likes roast beef’

'My kitty cat likes %s' % 'ham'

‘My kitty cat likes ham’

'My kitty cat fell on his %s' % 'head'

‘My kitty cat fell on his head’

"And now thinks he's a %s" % 'clam'

“And now thinks he’s a clam”

7.5

letter = """Dear {salutation} {name}
Thank you for your letter. We are sorry that out {product} {verbed} in your
{room}. Please note that it should never be used in a {room}, especially
near any {animals}.
Send us your receipt and {amount} for shipping and handling. We will sen
you another {product} that, in our tests, is {percent}% less likely to
have {verbed}.
Thank you for your support
Sincerely,
{spokesman}
{job_title}"""

7.6

response = dict()
response['salutation'] = '1'
response['name'] = '2'
response['product'] = '3'
response['verbed'] = '4'
response['room'] = '5'
response['animals'] = '6'
response['amount'] = '7'
response['percent'] = '8'
response['spokesman'] = '9'
response['job_title'] = '10'
print(letter.format(**response))

Dear 1 2

Thank you for your letter. We are sorry that out 3 4 in your

  1. Please note that it should never be used in a 5, especially near any 6.

Send us your receipt and 7 for shipping and handling. We will sen you another 3 that, in our tests, is 8% less likely to have 4.

Thank you for your support

Sincerely, 9 10

7.7

mammoth = """We have seen thee, queen of cheese,
Lying quietly at your ease,
Gently fanned by evening breeze,
Thy fair form no flies dare seize.
All gaily dressed soon you'll go
To the great Provincial show,
To be admired by many a beau
In the city of Toronto.
Cows numerous as a swarm of bees,
Or as the leaves upon the trees,
It did require to make thee please,
And stand unrivalled, queen of cheese.
May you not receive a scar as
We have heard that Mr. Harris
Intends to send you off as far as
The great world's show at Paris.
Of the youth beware of these,
For some of them might rudely squeeze
And bite your cheek, then songs or glees
We could not sing, oh! queen of cheese.
We'rt thou suspended from balloon,
You'd cast a shade even at noon,
Folks would think it was the moon
About to fall and crush them soon."""

7.8

import re
re.findall(r'\bc\w*\b', mammoth)

[‘cheese’, ‘city’, ‘cheese’, ‘cheek’, ‘could’, ‘cheese’, ‘cast’, ‘crush’]

7.9

re.findall(r'\bc.{3}\b', mammoth)

[‘city’, ‘cast’]

7.10

re.findall(r'\b\w+r\b', mammoth)

[‘your’, ‘fair’, ‘Or’, ‘scar’, ‘Mr’, ‘far’, ‘For’, ‘your’, ‘or’]

7.11

re.findall(r'\b\w*[aeiou]{3}[^aeiou]\w*\b', mammoth)

[‘queen’, ‘quietly’, ‘queen’, ‘squeeze’, ‘queen’]

7.12

import binascii
st = '4749463839610100010080000000000000ffffff21f9' + \
'0401000000002c000000000100010000020144003b'
gif = binascii.unhexlify(st)
gif

b’GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\x00\xff\xff\xff!\xf9\x04\x01\x00\x00\x00\x00,\x00\x00\x00\x00\x01\x00\x01\x00\x00\x02\x01D\x00;’

7.13

b = b'GIF89a'
gif[:6] == b

True

7.14

gif[6:10]

b’\x01\x00\x01\x00’

import struct
width, height = struct.unpack('<2H', gif[6:10])
print('width', width, 'height', height)

width 1 height 1

Comments