0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

[Python]左から一文字ずつ別の文字に置き換える

Posted at
------
*-----
**----
***---
****--
*****-
******

みたいなのがコンソールで書きたい。

結論

str.ljust()を使う。

n = int(input())
s = ""

for i in range(n+1):
    print(s.ljust(n, "-"))
    s += "*"
6
------
*-----
**----
***---
****--
*****-
******

ljust()はいわゆる0埋めとかに使われる関数で、「文字列の長さが第一引数に満たなかったら空いてるスペースを第二引数の文字で埋めるで~」というもの。

rjust()もあるが、両者の違いは 元の文字列をどちらに寄せるか という点。
ljust()は左、rjust()は右。わかりやすいね。

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?