zoukankan      html  css  js  c++  java
  • 21 pythone【入门指南】:string

    string是很基础的数据结构,来看看string有哪些常用的操作。

    #!/bin/python
    #!---encoding: UTF-8
    
    s = 'abcdefg'
    
    #concat
    s1 = s + "hi"
    print(s1)
    
    #find string pos
    #如果成功则返回对应index 如果查找失败则报异常 
    print(s.index("abc"))
    #print(s.index("ccc")) #ValueError: substring not found
    
    #slice
    print(s[1:])
    print(type(s), dir(str))

    通过dir来查看当前类的所有函数(包括私有函数,也包括公开函数)

    <class 'str'> ['__add__', '__class__', '__contains__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__gt__', '__hash__', '__init__', '__iter__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', 'capitalize', 'casefold', 'center', 'count', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'format_map', 'index', 'isalnum', 'isalpha', 'isdecimal', 'isdigit', 'isidentifier', 'islower', 'isnumeric', 'isprintable', 'isspace', 'istitle', 'isupper', 'join', 'ljust', 'lower', 'lstrip', 'maketrans', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']

    常用的有:join,count,encode,find,format,index,lower,upper,lstrip,rstrip,strip,replace,split,title

    操作1:lower,upper

    将字符串全部变成大/小写

    a = 'abcdef'
    b = a.upper() #ABCDEF
    c = b.lower() #abcdef

    操作2:lstrip,rstrip,strip

    strip:将string中左右出现的空白字符删除掉,并且如果是连续的空白字符,也一并删除,知道删除到不是空白字符为止,空白字符包含: , , 空格

    lstrip:将string中左边出现的连续空白字符删除掉,右侧的不管

    rstrip:将string中右侧出现的连续空白字符删除掉,左侧的不管

    a = "
    abc
    "
    print(a.strip()) #abc
    print(a.rstrip()) #
    abc
    print(a.lstrip()) #abc
    

    操作3:replace

    replace:将匹配的字符串替换为第二个参数中提供的字符串,注意replace不会改变原先的值,改变后的值需要用新的变量来接收

    #replace
    a = 'a b c'
    b = a.replace(' ', '-') #a-b-c
    print(a, b)

    操作4:title

    title:将字符串变成首字符大写,其它字符小写的格式,像文章段落的第一个单词那样。

    #title
    a = 'aBc'
    print(a.title()) #Abc

    操作5:count,find,index

    #count, find, index
    a = 'abcabc'
    print(a.count('abc')) #2
    print(a.count('abcd')) #0
    print(a.index('ab')) #0
    #print(a.index('abcd')) #ValueError: substring not found
    print(a.find('abc') #0
    print(a.find('abcd')) #-1

    count:统计字符串出现的次数

    find:查找该字符串是否在母字符串中出现过,出现过则返回对应的下标,否则返回-1;

    index:查找该字符串是否在木字符串中出现过,出现过则返回第一次出现的下标,否则抛出异常;

    操作6:join

    a = 'abc'
    print(a.join('---')) #-abc-abc-

    操作7:format,encode

    format:对象规定格式,参数来负责填充格式中的缺少的

    encode:字符串转码函数,参数是转码后的格式

    #format, encode
    print('{2},{1},{0}'.format('a', 'b', 'c')) #c,b,a
    print('abcd'.encode('UTF-8')) #b'abcd'

    操作8:split

    split:将按照参数中的字符将对象分割开来,返回list数组

    #split
    a = 'a,b,c'
    print(a.split(',')) #['a', 'b', 'c']

    还有两个常用的操作:连接,slice

    s1 = 'abcd'
    s2 = 'efgh'
    print(s1 + s2) #abcdefgh
    print(s1[1:]) #bcd
  • 相关阅读:
    python数据分析008_Matplotlib绘柱图,饼图,散点图
    python数据分析007_使用Matplotlib绘折线图
    python数据分析006_Python 2D绘图库Matplotlib
    python数据分析005_pandas的时间序列
    python数据分析004_多层索引的取值和排序
    python数据分析003_数据的合并筛选排序
    Megacli 简易使用
    k8s ingress 增加 跨域配置
    k8s 1.15 版本生产线上证书时间调整(亲测)
    grafana 展示 k8s prometheus
  • 原文地址:https://www.cnblogs.com/helww/p/9829900.html
Copyright © 2011-2022 走看看