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

  • 相关阅读:
    【Maven】项目打包-war包-Jar包[IDEA将项目打成war包]
    intellij idea 配置web 项目
    centos7启动iptables时报Job for iptables.service failed because the control process exited with error cod
    shell-运算符
    shell-流程控制
    shell-流程控制
    shell-变量,字符串,数组,注释,参数传递
    shell-变量,字符串,数组,注释,参数传递
    json解析
    json解析
  • 原文地址:https://www.cnblogs.com/mydesky2012/p/11507692.html
Copyright © 2011-2022 走看看