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

    1 定义

    ss = "aaaa", dd = 'ddddd'

    2 可以使用索引截取字符串

    cc = ss[0]  输出 'a'

    substr = dd[1:4]  [from:to]左闭右开,输出 'ddd',从下标1开始,截止到下标4

    3 *重复字符串

    sss = 'hello'*2,sss的值为:hellohello

    4 +拼接字符串

    add = ss + dd

    5 三引号表示多行字符串

    multi = ''' 

    第一行,

    第二行

    '''

    6 字符串内置函数

    str.find(self, str, from, end),检测子串是否包含在字符串中,可以使用from,end(end最大为len(self))来限制搜索范围(默认from=0,end=len(self))。from:end仍然为左开右闭。查到则返回第一次出现的下标位置,没有找到则返回-1。

    str.index(self, str, from, end),基本上同find函数功能相同,不同的是如果找不到str,则会报错。

    str.count(self, str, from, end),返回字符串出现的次数。没有出现,则返回0。

    str.rfind(self, str, from, end), 函数功能同find类似,只不过是从字符串右边开始查找。

    str.rindex(self, str, from, end),函数功能同index类似,只不过是从字符串右边开始查找。

    str.replace(self, old, new, max),max可选,表示最多替换次数不超过max。注意:replace返回一个新字符串。

    str.split(self, str),以某个字符串为分隔符分割字符串,结果为list。

    7 将数组转为分隔符分割的字符串

    idList = ["a","10","50"]
        print("-".join(idList))

    输出结果:a-10-50

  • 相关阅读:
    ElasticSearch工作原理
    prometheus监控es集群
    es索引调优
    ES中Refresh和Flush的区别
    网络服务器技术Apache与Nginx,IIS的不同
    shell里/dev/fd与/proc/self/fd的区别
    常用抓包工具
    Ubuntu Kubuntu Xubuntu Lubuntu Dubuntu Mythbuntu UbuntuBudgie区别
    Android的Looper.loop()消息循环机制
    申请读写sd卡权限shell
  • 原文地址:https://www.cnblogs.com/mydesky2012/p/11507692.html
Copyright © 2011-2022 走看看