• str( )它让Python将非字符串值表示为字符串



    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 byAppDataLocalProgramsPythonPython37python.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	byAppDataLocalProgramsPythonPython37python.exe C:/py3/rumen_shijian/str().py
    15 happy 23rd birthday!
    16 
    17 进程已结束,退出代码0
  • 相关阅读:
    JS
    JS
    JS
    VUE
    element-ui 进入页面 message 自动触发的问题
    JS-数组中常用的方法
    CSS-transition简单过渡动画
    vue
    JS
    2021要买的书籍
  • 原文地址:https://www.cnblogs.com/wl404/p/7880796.html
Copyright © 2020-2023  润新知