python:language_memo

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
python:language_memo [2019/06/10 17:19] – [文字列を日付型へ変換] ともやんpython:language_memo [2019/08/19 07:01] (現在) ともやん
行 1: 行 1:
 ====== Python ランゲージ メモ ====== ====== Python ランゲージ メモ ======
 +
 ===== 文字列 ===== ===== 文字列 =====
 +
 ==== 書式編集 ==== ==== 書式編集 ====
 <code python> <code python>
行 6: 行 8:
 str = "string value: '%s' integer value: %d" % (str_val, int_val) str = "string value: '%s' integer value: %d" % (str_val, int_val)
 str = "dic['key']: '%(key)s'" % { 'key' : 'abc' } str = "dic['key']: '%(key)s'" % { 'key' : 'abc' }
 +
 +"string value: '{}' integer value: {}".format('abc', 123)
 +"string value: 'abc' integer value: 123"
 +
 +str_val = 'abc'
 +int_val = 123
 +f"string value: '{str_val}' integer value: {int_val}"
 +"string value: 'abc' integer value: 123"
 +
 +int_val = 123456789
 +int_val1 = 12345
 +f'integer value: {int_val:,} integer value1: {int_val1:,}'
 +'integer value: 123,456,789 integer value1: 12,345'
 +
 +pi = 3.14159265358979
 +f'pi: {pi:.2f}'
 +'pi: 3.14'
 +
 +flt_val = 123456789.0123456
 +f'float value: {flt_val:,.4f}'
 +'float value: 123,456,789.0123'
 +
 +flt_val = 1234.5678
 +f'float value: {flt_val:>10,.2f}'
 +'float value:   1,234.57'
 +
 +'int: {0:d}; hex: {0:#x}; oct: {0:#o}; bin: {0:#b}'.format(65536)
 +'int: 65536; hex: 0x10000; oct: 0o200000; bin: 0b10000000000000000'
 </code> </code>
  
行 90: 行 120:
 ClsB.method: test ClsB.method: test
 </code> </code>
 +
 +===== 参考文献 =====
 +[[https://note.nkmk.me/python-format-zero-hex/|Python, formatで書式変換(0埋め、指数表記、16進数など) | note.nkmk.me]]\\
 +[[https://qiita.com/mas9612/items/af6a3030f9ef19feae22|Python3のstr.format()メソッドのいろんな書式指定]]\\
 +
  • python/language_memo.1560154748.txt.gz
  • 最終更新: 2019/06/10 17:19
  • by ともやん