1
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 5 years have passed since last update.

Pythonでbackspace(0x08)をprintすると前の文字が消える

Last updated at Posted at 2019-06-01

後輩が次のようなコードの挙動で悩んでいました。

user = User()
print(user.name)
# => ninomiyt

# ただの文字列
print(type(user.name)
# => str

print("Hello, {}!".format(user.name))
# => Hello, ninomiy! 

たしかに不審な挙動だったので調べたところ、

ことのコンボが決まった結果「実は{}の後ろにbackspaceの制御文字が混ざっていて一文字消えてしまう」ような現象が発生していたようです。

無事、以下のようなコードで再現することができました。変なバグじゃなくてよかった。

text = "text" + chr(0x08)
print(text)
# => tex

# 文字列としては保持している
print(len(text))
# => 5
1
1
1

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