Pythonでの文字列の書式設定操作は何ですか?

Pythonで文字列のフォーマット操作は、変数を文字列に挿入するためにformat()メソッドを使用するか、%演算子を使用して行います。以下は、文字列のフォーマットの例です:

format()メソッドを使用する:

name = "Alice"
age = 30
sentence = "My name is {} and I am {} years old".format(name, age)
print(sentence)

%演算子を使用する:

name = "Bob"
age = 25
sentence = "My name is %s and I am %d years old" % (name, age)
print(sentence)

これらの方法のいずれかを選択して使用すると、文字列のフォーマット操作を行うことができます。

コメントを残す 0

Your email address will not be published. Required fields are marked *


广告
広告は10秒後に閉じます。
bannerAds