0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pythonのエスケープシーケンス(escape sequence)について

Posted at

はじめに

Pythonのエスケープシーケンス(escape sequence)についてまとめてみました。

エスケープシーケンス(escape sequence)とは

 単一引用符(シングルクォーテーション)「'」は、文字列リテラルの最初と最後を表すための特別な文字です。そのため、例えば「'This isn't a pen.'」といった表記は行えません。
 文字「'」は、2個の文字「'」で表記します(見かけは「\」と「'」の2個の文字ですが、「'」という1個の文字を表します)。すなわち、「'This isn「\」't a pen.'」が正しい表記です。

✖ 「'This isn't a pen.'
〇 「'This isn「\」't a pen.'

 このような、逆斜線記号(バックスラッシュ)「\」を先頭にした、2個、あるいは、それ以上の個数の文字で、(通常の文字としては表記不能あるいは困難である)単一の文字を表す表記法が、エスケープシーケンス(escape sequence)です。

エスケープシーケンスの一覧

\a

警報(alert)
聴覚的または視覚的な警報を発する。

\b

後退(backspace)
表示位置を直前の位置へ移動する。

\f

書式送り(form feed)
改ページして、次のページの先頭へ移動する。

\n

改行(new line)
改行して、次の行の先頭へ移動する。

\r

復帰(carriage return)
現在の行の先頭位置へ移動する。

\t

水平タブ(horizontal tab)
次の水平タブ位置へ移動する。

\v

垂直タブ(vertical tab)
次の垂直タブ位置へ移動する。

\ \

逆斜線文字(バックスラッシュ) 「\」

\ ?

疑問符 「?」

\ '

単一引用符(シングルクォーテーション) 「'」

\ "

二重引用符(ダブルクォーテーション) 「"」

\newline

バックスラッシュと改行文字を無視する。

\ooo

oooは1~3桁の8進数
8進数でoooの値をもつ文字。

\xhh

hhは2桁の16進数
16進数でhhの値をもつ文字。

注意:日本語版のMS-Windowsでは、逆斜線\の代わりに円記号¥を使います。

おわりに

ここまで読んでいただきありがとうございます!

参考文献

・新・明解Python入門 (新・明解シリーズ) 単行本 – 2019/5/23
柴田 望洋 (著)
p.13

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

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?