1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 age = 23
5 message = "happy " + age +"rd birthday!" #python无法识别age,所以执行结果报错
6
7 print(message)
8
9 执行结果为:
10
11 C:\Users\tby\AppData\Local\Programs\Python\Python37\python.exe C:/py3/rumen_shijian/str().py
12 Traceback (most recent call last):
13 File "C:/py3/rumen_shijian/str().py", line 5, in <module>
14 message = "happy " + age +"rd birthday!"
15 TypeError: can only concatenate str (not "int") to str
16
17 进程已结束,退出代码1
1 #!/usr/bin/env python
2 # -*- coding:utf-8 -*-
3
4 age = 23
5 message = "happy " + str(age) +"rd birthday!" #str()它让Python将非字符串值表示为字符串,所以能打印出age的值2和3
6
7 print(message)
8
9
10
11 执行结果为:
12
13
14 C:\Users\tby\AppData\Local\Programs\Python\Python37\python.exe C:/py3/rumen_shijian/str().py
15 happy 23rd birthday!
16
17 进程已结束,退出代码0