計算機科学のブログ

Python - Diving in: Let's Make a Splash - string, methods, removesuffix, split, chain

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter2.(Diving in: Let’s Make a Splash)、SHARPEN YOUR PENCIL(69/82)の解答を求めてみる。

Jupyter(コード、入出力結果)

sameple3.ipynb

fn = 'Darius-13-100m-Fly.txt'
swimmer, age, distance, stroke = fn.removesuffix('.txt').split('-')
swimmer == 'Darius'
True
age == '13', distance == '100m', stroke == 'Fly'
(True, True, True)
print(swimmer)
print(age)
print(distance)
print(stroke)
Darius
13
100m
Fly
fn = 'Abi-10-50m-Back.txt'
swimmer, age, distance, stroke = fn.removesuffix('.txt').split('-')
swimmer == 'Abi', age == '10', distance == '50m', stroke == 'Back'
(True, True, True, True)
swimmer
'Abi'
age
'10'
distance, stroke
('50m', 'Back')