Python3.6でのString Object、色々
年末年始の休みも昨日でおしまい。
本日はPythonのString Objectについて学習。String Objectはたくさんの機能があって覚えきれないという印象。
print('Hello, World. ß'.casefold())
print('Hello, World. ß'.lower())
casefold()とlower()の違いについて、casefoldはドイツ語のßという文字もlowerケースに強引に変えられる。厳密にいうと、おそらくlowerケースではないのだと思うけど、ßがssに変換される。lower()では変換できず、そのままßで表示される。Hatena Blogではどうなるのかな?。
format()を利用した整形色々
x = 42 * 747
- print('The number is {:<09}'.format(x))
- print('The number is {:>09}'.format(x))
- print('The number is {:.3f}'.format(x))
- print('The number is {:,}'.format(x))
- print('The number is {:,.3f}'.format(x))
- print('The number is {:,}'.format(x).replace(',', '.'))
- The number is 313740000
- The number is 000031374
- The number is 31374.000
- The number is 31,374
- The number is 31,374.000
- The number is 31.374
こんな風に整形して表示できる
また
x = 100
print(f'This is {x}')
と表示することもPython 3.6以降で可能に。
リーダブルコード ―より良いコードを書くためのシンプルで実践的なテクニック (Theory in practice)
- 作者: Dustin Boswell,Trevor Foucher,須藤功平,角征典
- 出版社/メーカー: オライリージャパン
- 発売日: 2012/06/23
- メディア: 単行本(ソフトカバー)
- 購入: 68人 クリック: 1,802回
- この商品を含むブログ (140件) を見る