python 中字符串中含变量方法
Posted 好问雷
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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)运行结果:
以上是关于python 中字符串中含变量方法的主要内容,如果未能解决你的问题,请参考以下文章