My days of...

生活のことなど、がんばろう

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

  1. print('The number is {:<09}'.format(x))
  2. print('The number is {:>09}'.format(x))
  3. print('The number is {:.3f}'.format(x))
  4. print('The number is {:,}'.format(x))
  5. print('The number is {:,.3f}'.format(x))
  6. print('The number is {:,}'.format(x).replace(',', '.'))

 

  1. The number is 313740000
  2. The number is 000031374
  3. The number is 31374.000
  4. The number is 31,374
  5. The number is 31,374.000
  6. The number is 31.374

こんな風に整形して表示できる

また

x = 100
print(f'This is {x}')

と表示することもPython 3.6以降で可能に。

 

リーダブルコード ―より良いコードを書くためのシンプルで実践的なテクニック (Theory in practice)

リーダブルコード ―より良いコードを書くためのシンプルで実践的なテクニック (Theory in practice)

 
CODE VERSION 2.0

CODE VERSION 2.0