zoukankan      html  css  js  c++  java
  • python笔记-字符串连接

    字符串连接

    +

       1.Java中其他基本数据类型和string+,自动转成string处理

      Python中没有此特性。需要先转成string再做拼接

      2.每连接一次,就要重新开辟空间,然后把字符串连接起来,再放入新的空间

        大量字符串拼接时,效率低

    join

       'sep'.join(seq) ,seq必须是一个list,元组

       上面的语法即:以sep作为分隔符,将seq字符串序列中所有的元素合并成一个新的字符串

     

      *加号连接效率低是在连续进行多个字符串连接的时候出现的,如果连接的个数较少,加号连接效率反而比join连接效率高

    format

    str.format(text) 

    str中包含占位符,text中是要填充的内容

    使用'{}'占位符

    使用'{0}','{1}'形式的占位符

    使用'{name}'形式的占位符

    1 protocol='http'
    2 domain ='192.168.2.111'
    3 url = 'huice/event/api/add'
    4 data ="title='python大会'&time='2018-01-06'"
    5 print(protocol+'://' + domain + '/' + url + '/' + data)  # 使用加号连接字符串
    6 print("{0}://{1}/{2}/{3}".format(protocol,domain,url,data))    # 使用str.format(text) 格式化输出
    
    #打印结果:
    http://192.168.2.111/huice/event/api/add/title='python大会'&time='2018-01-06'
    http://192.168.2.111/huice/event/api/add/title='python大会'&time='2018-01-06'

    case='case01' desc='测试用例一' data='id=1' method ="""def test_{case}(self): {desc} execute_case({data}) """ method = method.format(case=case,desc=desc,data=data) print(method) # 打印结果: def test_case01(self): 测试用例一 execute_case(id=1) a=("2018","12","25") print('/'.join(a)) # 使用指定字符连接字符串 # 打印结果:2018/12/25

    字符串转列表

    str1 = "hi hello world"
    print(str1.split(" "))
    输出:
    ['hi', 'hello', 'world']

    2. 列表转字符串

    l = ["hi","hello","world"]
    print(" ".join(l))
    输出:
    hi hello world

     

  • 相关阅读:
    centos7装NVIDIA显卡驱动
    前端MVC学习笔记(三)——AngularJS服务、路由、内置API、jQueryLite
    前端MVC学习笔记(二)——AngularJS验证、过滤器、指令
    前端MVC学习笔记(一)——MVC概要与angular概要、模板与数据绑定
    JavaScript学习笔记(四)——jQuery插件开发与发布
    JavaScript学习笔记(三)——this、原型、javascript面向对象
    Node.js学习笔记——Node.js开发Web后台服务
    JavaScript学习笔记(二)——闭包、IIFE、apply、函数与对象
    JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
    【趣味分享】C#实现回味童年的24点算法游戏
  • 原文地址:https://www.cnblogs.com/lily1989/p/8462548.html
Copyright © 2011-2022 走看看