Atom's tech blog

Pythonを勉強しよう! シングル/ダブル/トリプルクォーテーションについて

f:id:iAtom:20201106203500j:plain

Python初心者の備忘録.....

今回は文字型には、「シングルクォーテーション」、「ダブルクォーテーション」、「トリプルクォーテーション」があり、このあたり勉強してします。

シングルクォーテーション/ダブルクォーテーション

」:シングルクォーテーション、「"」ダブルクォーテーションのどちらを使っても良いです。

文字列にしたい先頭と最後に記述します。

以下のケースでは「Atom」まで文字列になってしまうため、ダブルクォーテーションを使用する。

全文文字列にならない
name = 'Atom's tech blog'
全文文字列にする
name = "Atom's tech blog"
参考

ダブルクォーテーションが内側にある場合は、シングルクォーテーション、ダブルクォーテーションで囲む。

print('<a href="http://www.example.com"></a>')
print("<a href=\"http://www.example.com\"></a>")

# <a href="http://www.example.com"></a>
# <a href="http://www.example.com"></a>

トリプルクォーテーション

下記のように文字列が継続して改行している場合でも、文字列の続きとして扱われます。

改行している文字列自体もそのままの状態で扱われるため、複数行として表示します。

name ='''Atom's tech blog
test
proglam'''

print(name)

# Atom's tech blog
# test
# proglam