码迷,mamicode.com
首页 > 编程语言 > 详细

python 中字符串中含变量方法

时间:2018-04-29 13:26:26      阅读:190      评论:0      收藏:0      [点我收藏+]

标签:mat   结果   col   isp   for   http   src   spl   str   

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 中字符串中含变量方法

标签:mat   结果   col   isp   for   http   src   spl   str   

原文地址:https://www.cnblogs.com/hyl2018/p/8970769.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!