LoginSignup
18

More than 3 years have passed since last update.

[PowerShell / Python]フォーマット文字列ざっくりまとめ

Posted at

C/Perlとかのprintf()と比べると、PowerShellとPythonのフォーマット文字列が独特すぎてなかなかなじまないので、ざっくりと主要なものをまとめてみた

pritnf()

標準ライブラリでおなじみのフォーマット文字列。
Man page of PRINTF

文字列

printf "\"%8s\"\n", "hello";  # 右寄せ8桁
printf "\"%-8s\"\n", "hello"; # 左寄せ8桁

出力

"   hello"
"hello   "

数値

# 数値
$val = -1;
printf "%d\n", $val;    # val
printf "%u\n", $val;    # unsigned val
printf "%X\n", $val;    # HEX
printf "%x\n", $val;    # hex
printf "%4d\n", $val;   # 右寄せ4桁
printf "%04d\n", $val;  # 0埋め4桁
printf "%f\n", $val;    # float
printf "%8.3f\n", $val; # 全8桁・小数以下3桁

出力

-1
18446744073709551615
FFFFFFFFFFFFFFFF
ffffffffffffffff
  -1
-001
-1.000000
  -1.000

PowerShell

基本

指定の位置に{n}(nは引数の順序)を記述する。番号は必須

$name = "くれりん"
Write-Output("こんにちは、{0}さん。" -f $name)
$hello = "こんにちわ"
Write-Output("{1}、{0}さん。" -f $name, $hello)

出力

こんにちは、くれりんさん。
こんにちわ、くれりんさん。

文字列

$str1 = "hello"
$str2 = "world"
Write-Output("'{0,8}'" -f $str1)  # 右寄せ
Write-Output("'{0,-8}'" -f $str1) # 左寄せ

出力

'   hello'
'hello   '

数値

$val = 254
Write-Output("{0}" -f $val)
Write-Output("{0,8}" -f $val)         # 8桁右寄せ
Write-Output("{0:00000000}" -f $val)  # 8桁0埋め
Write-Output("{0:d8}" -f $val)        # 8桁0埋め

出力

254
     254
00000254
00000254

小数

$val = 21.2345678
Write-Output("{0:.0000}" -f $val)     # 小数4桁
Write-Output("{0:f4}" -f $val)        # 小数4桁
Write-Output("{0:0000.000}" -f $val)  # 整数4桁小数3桁

出力

21.2346
21.2346
0021.235

※こんな感じ(↓)に、書式指定子を使った整数部と小数部の両方の桁指定はできなさそう?

Write-Output("{0:f4.3}" -f $val)

基数変換

Write-Output("{0:x4}" -f $val)        # hex
Write-Output("{0:X4}" -f $val)        # HEX

出力

00fe
00FE

桁区切り

カンマで3桁区切り以外にも、ハイフン付き電話番号のような特定の書式を設定するにはとても簡単にできる。

$val = 16777216
Write-Output("{0:#,#}" -f $val)  # 3桁,区切り

$num = 09012345678
Write-Output("{0:000-0000-0000}" -f $num)
Write-Output("{0:###-####-####}" -f $num)

出力 (0でなく#を使うと0埋めされない)

16,777,216
090-1234-5678
90-1234-5678

Python

str.format()を使用する。
とくに指定がない限りPython2/Python3共通。

基本

指定の位置に{n}(nは引数の順序)を記述する。PowerShellと異なり番号は省略すると引数の順序とみなされる。

name = "くれりん"
print("こんにちわ、{}さん。".format(name))
hello = "こんにちわ"
print("{}、{}さん。".format(hello, name))
print("{1}、{0}さん。".format(name, hello))

出力

こんにちわ、くれりんさん。
こんにちわ、くれりんさん。
こんにちわ、くれりんさん。

文字列

str = "hello"
print("'{:<8}'".format(str))  # 左寄せ
print("'{:^8}'".format(str))  # センタリング
print("'{:>8}'".format(str))  # 右寄せ

出力

'hello   '
' hello  '
'   hello'

数値

dは省略可(:>8でも大丈夫)

print("{:d}".format(val))
print("{:>8d}".format(val))  # 右寄せ8桁
print("{:08d}".format(val))  # 0埋め8桁
254
     254
00000254

小数

fは省略すると表示がおかしい

val = 21.2345678
print("{:.4f}".format(val))   # 小数4桁
print("{:8.3f}".format(val))  # 全8桁・小数3桁

出力

21.2346
  21.235

また、内部で型のチェックは行われており、小数の値に対してdで書式指定しようとするとエラーとなる

print("{:d}".format(val))   # 整数

結果

Traceback (most recent call last):
  File "./print2.py", line 27, in <module>
    print("{:d}".format(val))   # 整数
ValueError: Unknown format code 'd' for object of type 'float'

キャストすれば動く

print("{:d}".format(int(val)))   # 整数

基数変換

val = 254
print("{:04x}".format(val))  # hex
print("{:04X}".format(val))  # HEX

出力

00fe
00FE

桁区切り

val = 16777216
print("{:,d}".format(val))

出力

16,777,216

%書式

実はprintf()同等の%を使った書式も使える。(ただしformatの方が間違いにくいということでformatの使用が推奨されている)

val = 21.2345678
print("%08.3f" % val)
print("%08d" % val) # こっちはcastなしで大丈夫
print("%8s" % str)

出力

0021.235
00000021
   hello

f文字列

Python3.6以降では、formatに変わるf文字列という機能が導入されている。
それ以前のバージョンでは使用できないので注意。
基本的にformatで書かれたコードは書き直せる。…と思う。

formatでは文字列中は{}と記述し、変数は引数で指定していたが、f文字列では{}内に変数名を記述するようになる。

name = "くれりん"
print(f"こんにちわ、{name}さん。")

# 文字列
str = "hello"
print(f"'{str:<8}'")  # 左寄せ
print(f"'{str:^8}'")  # センタリング
print(f"'{str:>8}'")  # 右寄せ
# 数値
val = 254
print(f"{val:d}")
print(f"{val:04x}")   # hex
print(f"{val:04X}")   # HEX
print(f"{val:>8}")  # 右寄せ8桁
print(f"{val:08}")  # 0埋め8桁
# 小数
val = 21.2345678
print(f"{val:.4f}")   # 小数4桁
print(f"{val:8.3f}")  # 全8桁・小数3桁
# 式
print(f"{1+5*5:d}")

出力

こんにちわ、くれりんさん。
'hello   '
' hello  '
'   hello'
254
00fe
00FE
     254
00000254
21.2346
  21.235
0026

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18