zoukankan      html  css  js  c++  java
  • python基础总结(字符串)

    1.常见操作1-查找

    string.find(str, beg=0, end=len(string))检测 str 是否包含在 string 中,如果 beg 和 end 指定范围,则检查是否包含在指定范围内,如果是返回开始的索引值,否则返回-1

    a="adsdfnjd"  b=a.find("s")    print(b)

    string.rfind(str, beg=0, end=len(string))类似于 find()函数,不过是从右边开始查找.

    a="adsdfnjd"  b=a.rfind("f")    print(b)

    string.index(str, beg=0, end=len(string))类似于 find()函数,但是找不到报异常.

    a="adsdfnjd"  b=a.index("q")    print(b)

    string.rindex(str, beg=0, end=len(string))类似于 rfind()函数,但是找不到报异常.

    a="adsdfnjd"  b=a.rindex("f")    print(b)

    2.常见操作2-统计

    string.count(str, beg=0, end=len(string))检测 str 是否包含在 string 中出现的次数,如果 beg 和 end 指定范围,则检查是否包含在指定范围内

    a="adsdsadasdffnjd"  b=a.count("s")    print(b)

    3.常见操作3-分隔

    string.split(str="", num=string.count(str))

    以 str 为分隔符切片 string,如果 num有指定值,则仅分隔 num 个子字符串

    a="adsdsadasdffnjd"  b=a.split("s")    print(b)

    string.splitlines([keepends])

    按照行(' ', ' ', ')分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

    a="adsd fssas anjd"    b=a.splitlines(keepends=False)   print(b)

    string.partition(str)有点像 find()和 split()的结合体,从 str 出现的第一个位置起,把 字 符 串 string 分 成 一 个 3 元 素 的 元 组 (string_pre_str,str,string_post_str),如果 string 中不包含str 则 string_pre_str == string.

    a="adsdfssasanjd"   b=a.partition("f")    print(b)

    string.rpartition(str)

    类似于 partition()函数,不过是从右边开始.

    a="adsdfssafsanjd"   b=a.rpartition("f")    print(b)

    4.常见操作4-判断

    string.startswith(obj, beg=0,end=len(string))

    检查字符串是否是以 obj 开头,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.

    a="adsdfssafsanjd"   b=a.startswith("f")    print(b)

    string.endswith(obj, beg=0,end=len(string))

    检查字符串是否是以 obj 结尾,是则返回 True,否则返回 False。如果beg 和 end 指定值,则在指定范围内检查.

    a="adsdfssafsanjd"   b=a.endswith("f")    print(b)

    string.isalnum()                 所有字符都是字母或数字则返回 True,否则返回 False

    a="adsdfssafsanjd"   b=a.isalnum()    print(b)

    string.isalpha()                  所有字符都是字母则返回 True,否则返回 False

    a="adsdfs+-/anjd"   b=a.isalpha()    print(b)

    string.isdigit()           所有字符都是数字则返回 True,否则返回 False

    a="adsdfs+-/anjd"   b=a.isdigit()    print(b)

    string.isupper()                  所有字符都是大写则返回 True,否则返回 False

    a="adsdfs+-/anjd"   b=a.isupper()    print(b)

    string.islower()                  所有字符都是小写则返回 True,否则返回 False

    a="adsdfs+-/anjd"   b=a.islower()    print(b)

    string.isspace()                  只包含空格则返回 True,否则返回 False

    a="adsdfs+-/anjd"   b=a.isspace()    print(b)

    5.常见操作5-大小写

    string.capitalize()  把字符串的第一个字符大写

    a="adsdfsanjd"   b=a.capitalize()    print(b)

    string.upper()   转换 string 中的小写字母为大写

    a="adsdfsanjd"   b=a.upper()    print(b)

    string.lower()   转换 string 中的大写字母为小写

    a="adsdfsanjd"   b=a.lower()    print(b)

    6.常见操作6-对齐

    string.ljust(width)  返回一个原字符串左对齐,并使用空格填充至长度 width 的新字符串

    a="adsdfsanjd"   b=a.ljust(20)    print(“%ssss”%b)

    string.rjust(width)  返回一个原字符串右对齐,并使用空格填充至长度 width 的新字符串

    a="adsdfsanjd"   b=a.rjust(20)    print(“%ssss”%b)

    string.center(width)   返回一个原字符串居中,并使用空格填充至长度 width 的新字符串

    a="adsdfsanjd"   b=a.center(20)    print(“%ssss”%b)

    7.常见操作7-裁剪

    string.strip([obj])  删除 string 字符串前后的的obj,如果不传参数,删除前后空格

    a="adsdfsanjda    "   b=a.strip(“a”)    print(b)

    string.lstrip([obj])  删除 string 字符串左面的obj,如果不传参数,删除左面空格

    a="adsdfsanjda"   b=a.lstrip(“s”)    print(b)

    string.rstrip([obj])  删除 string 字符串右面的obj,如果不传参数,删除右面空格

    a="adsdfsanjda"   b=a.rstrip(“s”)    print(b)

    8.     常见操作8-合并

    string.join(seq)  以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串

    a="adsdfsanjda"  c=”12555” b=a.join(c)    print(b)

    9.常见操作9-编码解码

    string.encode(encoding='UTF-8', errors='strict')

    以 encoding 指定的编码格式编码 string,如果出错默认报一个ValueError 的异常,除非 errors 指定的是'ignore'或者'replace'

    bytes.decode(encoding='UTF-8', errors='strict')

    a='我'   b=a.encode("UTF-8")    print(b)

    以 encoding 指定的编码格式解码 string,如果出错默认报一个 ValueError 的 异 常 , 除 非 errors 指 定 的 是 'ignore' 或 者'replace'

    a=b'xe6x88x91'   b=a.decode("UTF-8")  print(b)



    注意:本篇文章是个人总结,仅供参考。若出现其他问题,与写者无关,切勿用于商业获取福利!

       如果总结有勿,或者交流与学习,请与写着联系!qq:1349176744

  • 相关阅读:
    Jmeter性能测试--自己看到的博客收集
    python日志logging
    java学习day12--API-File文件流
    java学习day12--API-IO简介--流简介
    java学习day12--API-SimpleDateFormat--BigDecimal/BigInteger类
    java学习day12--API-包装类-Date类
    equals方法和 == 的使用
    java学习day11--API--Object类-String类-StringBuilder类
    构造方法和普通方法的区别
    java中的修饰符总结
  • 原文地址:https://www.cnblogs.com/quietly-elegant/p/10283557.html
Copyright © 2011-2022 走看看