pythonに慣れよう3

# coding: utf-8
name = "Gennadiy"
skill = "3D modeling"
location = "West Hollywood, Russia"

print("name: %s, skill: %s, location: %s" % (name, skill, location))

[vagrant@localhost python]$ python app.py
name: Gennadiy, skill: 3D modeling, location: West Hollywood, Russia

この記法は、mysqlのset valueみたいですね。変数をstringからnumberに変えます。

# coding: utf-8
name = "Gennadiy"
skill = "3D modeling"
age = 18

print("name: %s, skill: %s, age: %d" % (name, skill, age))

[vagrant@localhost python]$ python app.py
name: Gennadiy, skill: 3D modeling, age: 18

%f はfloatになるので、コンパイルエラーになります。
Traceback (most recent call last):
File “app.py”, line 6, in
print(“name: %s, skill: %s, age: %f” % (name, skill, age))
TypeError: float argument required, not str

型指定しない記法もある。

print("name: {0}, skill: {1}, age: {2}".format(name, skill, age))