自学内容网 自学内容网

python学习笔记—7—变量拼接

1. 字符串的拼接 print(var_1 + var_2)  

print("supercarry" + "doinb")
name = "doinb"
sex = "man"
score = "100"
print("sex:" + sex + " name:" + name + " score:" + score)

        注意:+ 只适用于字符变量和字面量的拼接,不能将整数、浮点数和字符串混在一起,否则会报错

2. 通过占位符实现各种变量的拼接

        “%占位符” % 变量

(1)%d 整数占位符

(2)%f 浮点数占位符

(3)%s 字符串占位符

name = "doinb"
print("this is supercarry%s" % name)
print("%s is Champion , this is supercarry%s" % (name, name))

score_1 = 100
score_2 = 60
print("the score of doinb is %s" % score_1)
print("the score of doinb is %d     the score of lwx is %d" % (score_1, score_2))

salary_1 = 6666.66
salary_2 = 100000000
print("the salary of doinb is %f" % salary_1)
print("the score of doinb is %f     the score of lwx is %f" % (salary_1, salary_2))

print("%s is Champion, the score of %s is %d, the salary of %s is %f" % (name, name, score_1, name, salary_2))

注意:整数、浮点数可通过%s转化为字符串


原文地址:https://blog.csdn.net/qq_47798402/article/details/144433458

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!