age = input("Age:") job = input("Job:") hobby = input("Hobbie:") info = ''' ---------------info of %s ------------ Name : %s Age : %s job : %s Hobbie : %s ----------------end-------- ''' %(name,name,age,job,hobby) print(info)
%s 是字符串占位符, %d是数字占位符,若把上面的age换成%d,就只能输入数字,不然会报错。python
执行结果:ide
C:\Users\Administrator\Desktop>python test.py Name:xx Age:55 Job:tea Hobbie:dd ---------------info of xx ------------ Name : xx Age : 55 job : tea Hobbie : dd ----------------end--------
若是在字符串中出现了%s这样的占位符,那么全部的%都将变成占位符,若是像下面这样输入必定会报错:学习
print("我叫%s, 今天37岁,我正在教python,你已经学习了2%了" % '高胜寒')
输出结果:orm
C:\Users\Administrator\Desktop>python test.py Traceback (most recent call last): File "test.py", line 3, in <module> print("我叫%s, 今天37岁,我正在教python,你已经学习了2%了" % '高胜寒') TypeError: not enough arguments for format string
因此遇到这样的问题,咱们须要用%%来表示字符串中的%字符串
print("我叫%s, 今天37岁,我正在教python,你已经学习了2%%了" % '高胜寒')
输出结果:input
C:\Users\Administrator\Desktop>python test.py 我叫高胜寒, 今天37岁,我正在教python,你已经学习了2%了