Python3 文字列に変数を埋め込むサンプル

環境
Windows 11 Pro 64bit
Python 3.11

構文
“文字列{}".format(引数)

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
a = "toto"
b = "sohu"
print("This is {}.That is {}.".format(a, b))
# coding: utf-8 a = "toto" b = "sohu" print("This is {}.That is {}.".format(a, b))
# coding: utf-8

a = "toto"
b = "sohu"

print("This is {}.That is {}.".format(a, b))

実行結果
This is toto.That is sohu.

構文
f"文字列{変数}"
f文字列は値を埋め込んで文字列を結合します。

使用例

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
# coding: utf-8
a = "rss"
b = "boo"
print(f"This is {a}.That is {b}.")
# coding: utf-8 a = "rss" b = "boo" print(f"This is {a}.That is {b}.")
# coding: utf-8

a = "rss"
b = "boo"

print(f"This is {a}.That is {b}.")

実行結果
This is rss.That is boo.

IT

Posted by arkgame