zoukankan      html  css  js  c++  java
  • python字符串拼接

    1、脚本

    num =7

    #string和int拼接

    print ("there's",num,"boys" )

    #string和string拼接

    #(1)直接用+号

    print ("there's"+" "+"boys" )

    #(2)用逗号,

    print ("there's","boys" )

     备注:一般连接字符串用+ 而不是逗号? 

    因为我的目的是将两个字符串连接起来,组建成一个新的字符串。print 里的逗号其实是分隔参数的一种方式

    2、执行结果

    3、脚本

    # -*- coding: utf-8 -*-

    Her_name = 'babi'
    Her_high = 160
    Her_weight = 100

    print ("this is my friend",Her_name)
    print ("她",Her_high,"厘米 高")
    print ("she's",Her_weight,"jin")

    4、执行结果

    5、脚本

    # -*- coding: utf-8 -*-

    name = 'babi'
    high = 160
    weight = 100

    print ("this is my friend %s."%name)
    print ("she's %d high."%high)
    print ("she's %d jin."%weight)
    print ("she is",name,",",high, "cm high,and her weight is",weight )
    print ("she is %s,%d cm high and %d jin"%(name, high, weight))

    6、执行结果

    7、脚本(主要区分%s和%r; 可以换行)

    # -*- coding: utf-8 -*-

    name = "balabala'ha then"

    print ("this is my friend %s."%name)
    print ("this is my friend %r."%name)

    8、执行结果

    备注:

    (1)%s, %r , %d 这些符号是啥意思? 它们是一种“格式控制工具”。

           它们告诉 Python 把右边的变量带到字符串中,并且把变量值放到 %s 所在的位置上

           %r 就是是常有用的一个,它的含义是“不管什么都打印出来

    (2)%r 和 %s 有什么不同?
            %r 用来做 debug 比较好,因为它会显示变量的原始数据(raw data),而其它的符号则是用来向用户显示输出的
    (3)脚本7的string变量用双引号---------因为字符串中有特殊字符 ',如果用但引号,会报错

    (4)%r可以将变量的原型输出来

    (5)%s输出用户一般看到的格式

    (6) 可以换行

  • 相关阅读:
    解决编程开发时候文件命名的苦恼(规范代码 提高可读性)
    react的this.setState没有触发render
    动态修改JS对象的值及React setState
    Antd Select组件结合使用出现must set key for <rc-animate> children问题
    antd Select进阶功能 动态更新、函数防抖
    前端性能优化之重排和重绘
    ES5 map循环一大坑:循环遍历竟然出现逗号!
    Monent.js:强大的日期处理类库
    Docker概览
    Spring boot 集成hessian
  • 原文地址:https://www.cnblogs.com/merry-0131/p/8409630.html
Copyright © 2011-2022 走看看