1. 程式人生 > >python 中字符串中含變量方法

python 中字符串中含變量方法

hid 運行 nth clas format abc gif ima pen

1. 簡單粗魯的字符串拼接

1 name = "abc"
2 age = 25
3 info = "the name is "+name +"\nthe age is " + str(age)
4 print(info)

運行結果:

技術分享圖片

2.%

技術分享圖片
name = "abc"
age = 25
#info = "the name is "+name +"\nthe age is " + str(age)
info = "the name is %name \nthe age is %age"
print(info)
View Code

運行結果:

技術分享圖片

3.formate

name = "
abc" age = 25 #info = "the name is "+name +"\nthe age is " + str(age) # info = "the name is %name \nthe age is %age" info_1 = "the name is {name_} \nthe age is {age_}".format(name_=name ,age_= age) print(info_1) info_2 = "the name is {0} \nthe age is {1}".format(name ,age) print(info_2) info_3 = "the name is {} \nthe age is {}
".format(name ,age) print(info_3)

運行結果:

技術分享圖片

python 中字符串中含變量方法