python-字符串中含有變量的表示方法


 

name='liming'
age = 20

#1 字符串拼接,變量age必須是str類型
print('he is '+str(age)+' years old')
執行結果
he is 20 years old

#2 %s代表變量,
print('he is %s years old' % age)
執行結果
he is 20 years old

#3 format
print('{} is {} years old'.format(name,age))
print('{0} is {1} years old'.format(name,age))
print('{name_} is {age_} years old'.format(name_=name,age_=age))
執行結果
liming is 20 years old
liming is 20 years old
liming is 20 years old

 


免責聲明!

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



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