計算機科学のブログ

ほしい物リスト

Python - Deployment: Run Your Code Anywhere - string, methods, removesuffix, split

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 8.(Deployment: Run Your Code Anywhere)、SHARPEN YOUR PENCIl(395/682)の解答を求めてみる。

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

webapp/WebappSupport.ipynb

name = 'Katie-9-100m-Free.txt'
name.removesuffix('.txt')
'Katie-9-100m-Free'
name.removesuffix('.txt').split('-')
['Katie', '9', '100m', 'Free']
help(str.split)
Help on method_descriptor:

split(self, /, sep=None, maxsplit=-1) unbound builtins.str method
    Return a list of the substrings in the string, using sep as the separator string.

      sep
        The separator used to split the string.

        When set to None (the default value), will split on any whitespace
        character (including \n \r \t \f and spaces) and will discard
        empty strings from the result.
      maxsplit
        Maximum number of splits.
        -1 (the default value) means no limit.

    Splitting starts at the front of the string and works to the end.

    Note, str.split() is mainly useful for data that has been intentionally
    delimited.  With natural text that includes punctuation, consider using
    the regular expression module.
name.removesuffix('.txt').split('-', maxsplit=2)
['Katie', '9', '100m-Free']
name.removesuffix('.txt').split('-', 2)[-1]
'100m-Free'