ユーニックス総合研究所

  • home
  • archives
  • python-double-quote-escape

Pythonのダブルクォーテーションのエスケープ方法

  • 作成日: 2023-06-06
  • 更新日: 2023-12-24
  • カテゴリ: Python

Pythonでダブルクォーテーションをエスケープする

Pythonの文字列でダブルクォーテーションをエスケープするにはバックスラッシュを使います。
たとえば以下のようにです。

s = "abc\"def\"ghi"  
print(s)  # abc"def"ghi  

関連記事
Pythonのバックスラッシュ「\」の活用方法【エスケープ、複数行の式】

ちなみにシングルクォーテーションの文字列ではダブルクォーテーションはエスケープ不要です。

s = 'abc"def"ghi'  
print(s)  # abc"def"ghi  

さらにdocstringの場合もエスケープは不要です。

s = """abc"def"ghi"""  
print(s)  # abc"def"ghi  

s = '''abc"def"ghi'''  
print(s)  # abc"def"ghi