python 中字符串中含變量方法


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 %s \nthe age is %s"%(name ,age)
print(info)

 

運行結果:

 

 

3.formate

name = "abc"
age = 25
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)

運行結果:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM