LoginSignup
0
0

Pythonで「文字列を指定した長さにして左寄せ/中央揃え/右寄せを行う(ljust, center, rjust, zfill)」の動作を確認してみた

Posted at

概要

Pythonで「文字列を指定した長さにして左寄せ/中央揃え/右寄せを行う(ljust, center, rjust, zfill)」の動作を確認してみました。以下のページを参考にしました。

実装

以下のファイルを作成しました。

sample.py
print("[" + "Apple".ljust(8) + "]")
print("[" + "Apple".center(8) + "]")
print("[" + "Apple".rjust(8) + "]")
print("")
print("Apple".ljust(8, "_"))
print("Apple".center(8, "p"))
print("Apple".rjust(8, "+"))
#print("Apple".rjust(8, "*+"))
print("")
print("Apple".ljust(3))
print("Apple".center(3))
print("Apple".rjust(3))
print("")
print("89".zfill(5))
print("abc".zfill(6))
print("-123".zfill(8))
print("+975".zfill(8))
print("")
print("12345".zfill(3))

以下のコマンドを実行しました。

$ python3 sample.py 
[Apple   ]
[ Apple  ]
[   Apple]

Apple___
pApplepp
+++Apple

Apple
Apple
Apple

00089
000abc
-0000123
+0000975

12345

まとめ

何かの役に立てばと。

0
0
0

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
0
0