Pythonのシングルクォーテーションとダブルクォーテーションの違い
小粋空間 » Programming Language » Python » Pythonのシングルクォーテーションとダブルクォーテーションの違い
Pythonのシングルクォーテーションとダブルクォーテーションの違いについて調べてみました。
1.はじめに
2.シングルクォーテーションとダブルクォーテーションの違い
シングルクォーテーションとダブルクォーテーションの違いは特にありません。
PHPなどでは変数を展開する場合にダブルクォーテーションで括る必要がありますが、Pythonはそのような仕様はないようです。
PHPの場合
$str = 'World';
echo "Hello $str";
またPythonでは下記の考慮が必要です。
シングルクォーテーション:文字列にシングルクォーテーションが含まれる場合、エスケープが必要
str = '\'Hello World\''
ダブルクォーテーション:文字列にダブルクォーテーションが含まれる場合、エスケープが必要
str = "\"Hello World\""
言い換えると、逆のパターンであればエスケープは不要です。
str = "'Hello World'"
または
str = '"Hello World"'