• python字符串函数


    字符串 S="python string” 

    print str[0:3] #截取第一位到第三位的字符

    print str[:] #截取字符串的全部字符

    print str[6:] #截取第七个字符到结尾

    print str[:-3] #截取从头开始到倒数第三个字符之前

    print str[2] #截取第三个字符

    print str[-1] #截取倒数第一个字符


    print str[::-1] #创造一个与原字符串顺序相反的字符串


    print str[-3:-1] #截取倒数第三位与倒数第一位之前的字符


    print str[-3:] #截取倒数第三位到结尾

    字符串中的搜索和替换

    S.find(substr, [start, [end]])   #返回S中出现substr的第一个字母的标号,如果S中没有substr则返回-1。start和end作用就相当于在S[start:end]中搜索 
    S.index(substr, [start, [end]])   #与find()相同,只是在S中没有substr时,会返回一个运行时错误 
    S.count(substr, [start, [end]])    #计算substr在S中出现的次数 
    S.replace(oldstr, newstr, [count])    #把S中的oldstr替换为newstr,count为替换次数。这是替换的通用形式,还有一些函数进行特殊字符的替换 
    S.strip([chars]) #把S中前后chars中有的字符全部去掉,可以理解为把S前后chars替换为None 
    S.lstrip() 去掉左边所有空格
    S.rstrip() 去掉右边所有空格
    S.expandtabs([tabsize])   #把S中的tab字符替换没空格,每个tab替换为tabsize个空格,默认是8个

    字符串中字符大小写的变换
    S.lower()   #小写 
    S.upper()   #大写 
    S.swapcase()   #大小写互换 
    S.capitalize()   #首字母大写 
    String.capwords(S)  #这是模块中的方法。它把S用split()函数分开,然后用
    capitalize()把首字母变成大写,最后用join()合并到一起
    S.title()    #只有首字母大写,其余为小写,模块中没有这个方法

    字符串去空格及去指定字符
    去两边空格:str.strip()
    去左空格:str.lstrip()
    去右空格:str.rstrip()
    去两边字符串:str.strip('d'),相应的也有lstrip,rstrip
    str=' python String function '
    print '%s strip=%s' % (str,str.strip())
    str='python String function'
    print '%s strip=%s' % (str,str.strip('d'))

    按指定字符分割字符串为数组:str.split(' ')


    字符串编码和解码的函数:
    S.startwith(prefix[,start[,end]]) #是否以prefix开头 
    S.endwith(suffix[,start[,end]])  #以suffix结尾 
    S.isalnum()  #是否全是字母和数字,并至少有一个字符 
    S.isalpha()  #是否全是字母,并至少有一个字符 
    S.isdigit()  #是否全是数字,并至少有一个字符 
    S.isspace() #是否全是空白字符,并至少有一个字符 
    S.islower() #S中的字母是否全是小写 
    S.isupper() #S中的字母是否便是大写 
    S.istitle() #S是否是首字母大写的

    数字变为字符串 str()
    字符串变为数字 string.atoi(s,[,base]) //base为进制基数
    浮点数转换 string.atof(s)


    字符串在输出时的对齐:
    S.ljust(width,[fillchar])   #输出width个字符,S左对齐,不足部分用fillchar填充,默认的为空格。 
    S.rjust(width,[fillchar])    #右对齐 
    S.center(width, [fillchar])    #中间对齐 
    S.zfill(width)   #把S变成width长,并在右对齐,不足部分用0补足

    字符串中的单引号,双引号用 来转义。

  • 相关阅读:
    同余关系 等价关系 同余关系的原型
    同态 同构
    In abstract algebra, a congruence relation (or simply congruence) is an equivalence relation on an algebraic structure (such as a group, ring, or vector space) that is compatible with the structure in
    数学用语中的 透明 transitive property
    Sethi model
    非奇异的;非退化的;满秩
    非齐次线性方程组 引出线性流形 陪集
    field, or, more generally, in a ring or even a semiring 数域、环、半环
    Linear transformations. 线性变换与矩阵的关系
    Isomorphism 同构
  • 原文地址:https://www.cnblogs.com/qq991025/p/11597172.html
Copyright © 2020-2023  润新知