zoukankan      html  css  js  c++  java
  • Python常用的字符串方法总结

    Python常用的字符串类型:

    1、strip()

    描述:用于脱去字符串两边指定的字符(默认为空格)

    参数:chars  要脱去的子字符串

    返回值:返回脱去子字符串后而生成的新字符串

    示例1:

    name = "          xiaobai    "
    name = name.strip()
    print(name)
    

     输出结果:

    xiaobai
    View Code

    示例2:

    name = "******xiaobai********"
    name = name.strip("*")
    print(name)
    

      输出结果:

    xiaobai
    View Code

    2、capitalize()

    描述:将字符串的首字母大写

    参数:

    返回值:返回一个新的首字母大写的字符串对象。

    示例:

    s = 'hello world'
    s = s.capitalize()  # 将s字符串的首字母大字
    print(type(s),s)
    

     输出结果:

    <class 'str'> Hello world
    View Code

    3、upper()

    描述:将字符串中的小写字母转为大写字母

    参数:

    返回值:返回小写字母转为大写字母后的字符串

    示例:

    s = 'hello world'
    s = s.upper()
    print(type(s),s)
    

      输出结果:

    <class 'str'> HELLO WORLD
    View Code

    4、center()

    描述:让字符串居中对齐

    参数:width  宽度,返回字符串长度

       fillchar  填充字符,不填默认以空格为填充字符

    返回值:返回新的居中对齐的字符串

    示例1:

    s = 'hello world'
    s = s.center(26)   # 字符串在26个空格中间
    print(type(s),'起始位置',s,'结束位置')
    

       输出结果:

    <class 'str'> 起始位置        hello world         结束位置
    View Code

    示例2:

    s = 'hello world'
    s = s.center(26,'#')  # 字符串在26个"#"号中间
    print(type(s),s)
    

     输出结果:

    <class 'str'> #######hello world########
    View Code

    5、count()

    描述:用于统计字符串里指定字符出现的次数。

    参数:sub  查询的子字符串

         start 开始索引。默认为第一个字符,第一个字符索引值为0

         end  结束索引。字符中第一个字符 的索引为0。默认字符串的最后一个位置

    返回值:返回整型,指定的字符在字符串中出现的次数。

    示例1:

    s = 'hello world'
    s = s.count('l')
    print(type(s),s)
    

     输出结果:

    <class 'int'> 3
    View Code

    示例2:

    s = 'hello world'
    s = s.count('l',3,10) # 表示统计字母“l”,从索引位置3到9位置(顾首不顾尾)的子串的个数
    print(type(s),s)
    

     输出结果:

    <class 'int'>   2
    View Code

    6、encode()

    描述:以指定的编码格式编码的字符串

    参数:encoding  要使用的字符编码类型,如:UTF-8

       errors  设置不同错误的处理方案。默认为strict

    返回值:返回指定编码的字节流(uncode编码)

    示例1:

    s = 'hello world'
    s = s.encode('ascii') # 将字符串编码成ascii格式的编码字符串
    print(type(s),s)
    

     输出结果:

    <class 'bytes'> b'hello world'
    View Code

    示例2:

    s = '世界你好'
    s = s.encode('utf-8')
    print(type(s),s)
    

     输出结果:

    <class 'bytes'> b'\xe4\xb8\x96\xe7\x95\x8c\xe4\xbd\xa0\xe5\xa5\xbd'
    View Code

    示例3:

    s = '世界你好'
    s = s.encode('gbk')
    print(type(s),s)
    

      输出结果:

    <class 'bytes'> b'\xca\xc0\xbd\xe7\xc4\xe3\xba\xc3'
    View Code

    7、endswith()

    描述:判断是否以指定的子串结尾

    参数:suffix  子串

       start  开始索引

       end  结束索引  

    返回值: 返回布尔值

    示例1:

    s = 'hello world'
    s = s.endswith('rld')
    print(type(s),s)
    

     输出结果:

    <class 'bool'> True
    View Code

    示例2:

    s = 'hello world'
    s = s.endswith('or',1,9) # 表示从索引位置1至8(顾首不顾尾)查找是否以字符'or'结尾
    print(type(s),s)
    

     输出结果:

    <class 'bool'> True
    View Code

    8、startswith()

    描述:检测字符串是否以指定子串开头,如果是则返回True,否则返回False

    参数:prefix  指定的子串

       start  开始的位置

       end  结束的位置

    返回值:返回一个布尔值

    示例:

    s = 'hello world'
    s = s.startswith('llo',2,10)  # 表示从索引2位置至9的位置搜索是否有以字符“llo”开头的
    print(type(s),s)
    

      输出结果:

    <class 'bool'> True
    View Code

    9、find()

    描述:检测字符串中是否包含指定的子串

    参数:sub  要查找的子串

       start 查找的开始位置

       end  查找的结束位置

    返回值:若包含子串则返回开始的索引值,否则返回-1

    示例1:

    s = 'hello world'
    s = s.find('l',5,10) # 查找索引5至9位置中的字符“l”的位置
    print(type(s),s)
    

     输出结果:

    <class 'int'> 9
    View Code

    示例2:

    s = 'hello world'
    s = s.find('x') # 查找一个字符串中不存在的子串
    print(type(s),s)
    

     输出结果:

    <class 'int'> -1
    View Code

    10、format()

    描述:字符串的格式化输出。

    种类:分别是%{} 两种占位符格式控制符

    常用的数据格式类型:

    (1) %s  字符串                重点

    (2)%d  有符号整数(十进制)       重点

    (3)%f  浮点数(用小数点符号)

    示例1:

    s = 'My name is {0},My age is {1}'
    s1 = s.format('xiaobai',18)
    print(type(s1),s1)   
    # format会把传入的参数按位置顺序来填充,第一个参数是下标0,然后是1...一一对应
    

    输出结果:

    <class 'str'> My name is xiaobai,My age is 18
    View Code

    示例2:

    s = "Hello, my name is {name},I'm {age} year old,I am a {profession}"
    s1 = s.format(profession='programmer',age=18,name='xiaobai')
    print(type(s1),s1)
    # format通过字典的key来填充,括号中可以不按顺序传参。
    

    输出结果:        

    <class 'str'> Hello, my name is xiaobai,I'm 18 year old,I am a programmer
    View Code

    示例3:

    s = 'my name is %s,my age is %d'%('xiaobai',18)
    print(type(s),s)# 用%号进行字符串格式化
    

    输出结果:

    <class 'str'> my name is xiaobai,my age is 18
    View Code

    11、index()

    描述:查找字符串,返回子串的索引,使用方法与find相同,不同之处是若查找的子串不存在会报错

    参数:sub  查找的子字符串

         start  查找的起始位置

         end  查找的结束位置

    返回值:若包含子字符串则返回开始的索引值,否则抛出异常。

    示例1:

    s = 'hello world'
    s = s.index('world') # 如果要查找的子字符串没有,则会抛出异常。
    print(type(s),s)
    

    输出结果:

    <class 'int'> 6
    View Code

    示例2:

    s = 'hello world'
    s = s.index('o',5,10) # 表示从索引5至索引10的位置的开始查找字符“o“的所在的索引位置。
    print(type(s),s)
    

    输出结果:

    <class 'int'> 7
    View Code

    12、isdigit()

    描述:判断一个字符串中是否由数字组成

    参数:

    返回值:返回一个布尔值(如果由数字组成则返回True,否则返回False)

    示例:

    s = '123456789'
    s = s.isdigit()
    print(type(s),s)
    

    输出结果:

    <class 'bool'> True
    View Code

    13、islower()

    描述判断字符串是否全由写字母组成

    参数:

    返回值:返回一个布尔值(若字母都是小写则返回True,否则返回False)

    示例:

    s = 'xiaobai888888'
    s = s.islower()
    print(type(s),s)
     
    s2 = '88888hello world88888'
    s2 = s2.islower()
    print(type(s2),s2)
    

    输出结果:

    <class 'bool'> False
    <class 'bool'> True
    View Code

    14、isspace()

    描述:检测字符串是否只由空格、tab、回车组成

    参数:

    返回值:返回布尔值

    示例1:

    s = '      '
    s = s.isspace()
    print(type(s),s)
     

    输出结果:

    <class 'bool'> True
    View Code

    示例2:

    s = '\t\n'
    s = s.isspace()
    print(type(s),s)
     

    输出结果:

    <class 'bool'> True
    View Code

    15、istitle()

    描述:判断字符串中的每个单词首字母是否为大写

    参数:

    返回值:返回一个布尔值(字符串中的每个单词首字母为大写则返回True,否则返回False)

    示例:

    s = '888888What Is Your Name?'
    s = s.istitle()
    print(type(s),s)
     
    s2 = '888888what is your name?'
    s2 = s2.istitle()
    print(type(s2),s2)
    

    输出结果:

    <class 'bool'> True
    <class 'bool'> False
    View Code

    16、isupper()

    描述:判断字符串是否全是大写

    参数:

    返回值:返回一个布尔值

    示例:

    s = '88888MY NAME IS WILLIAM'
    s = s.isupper()
    print(type(s),s)
     
    s2 = 'my name is William'
    s2 = s2.isupper()
    print(type(s2),s2)
    

    输出结果:

    <class 'bool'> True
    <class 'bool'> False
    View Code

    16、join()

    描述:将序列中的元素以指定的字符连接,生成一个新的字符串。

    参数:iterable  指定的字符

    返回值:返回通过指定字符连接序列中元素后生成的新字符串

    示例:

    s1 = '-'
    s2 = ''
    seq = ('h','e','l','l','o')
    print(s1.join(seq))
    print(s2.join(seq))
    

    输出结果:

    h-e-l-l-o
    hello
    View Code

    17、ljust()

    描述:左对齐,右填充

    参数:width  指定字符串长度。

       fillchar  填充字符,默认为空格。

    返回值:返回一个原字符串右对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。

    示例:

    s = 'hello world'
    s = s.ljust(50,'*')
    print(type(s),s)
    

     输出结果:

    <class 'str'> hello world***************************************
    View Code

    18、lower()

    描述:将字符串中的大写字母替换成小写

    参数:

    返回值:返回一个小写的字符串

    示例:

    s = 'HELLO WORLD'
    s = s.lower()
    print(type(s),s)
    

    输出结果:

    <class 'str'> hello world
    View Code

    19、replace()

    描述:子串替换

    参数:old  要替换的字符串

         new  新的字符串

         count  替换次数

    返回值:去掉字符串中的旧字符串,替换成新字符串后生成一个新的字符串。

    示例1:

    s = 'hello world'
    s = s.replace('world','python')
    print(type(s),s)
    

    输出结果:

    <class 'str'> hello python
    View Code

    示例2:

    s = 'hello world world'
    s = s.replace('world','python',1)  # 只替换第一个world
    print(type(s),s)
    

    输出结果:

    <class 'str'> hello python world
    View Code

    20、split()

    描述:分隔字符串

    参数:sep  分隔符

         maxsplit  最多分隔的次数

    返回值:返回分割后的字符串列表

    示例1:

    s = 'hello world'
    s = s.split()    # 括号内什么不加默认以空格为分隔符
    print(type(s),s)# 得到一个列表
    

    输出结果:

    <class 'list'> ['hello', 'world']
    View Code

    示例2:

    s = 'hello world'
    s = s.split('l') # 表示以字符"l"为分隔符
    print(type(s),s)# 得到一个列表
    

    输出结果:

    <class 'list'> ['he', '', 'o wor', 'd']
    View Code

    21、swapcase()

    描述:字符串中的大小写互换

    参数:

    返回值:返回一个字母大小写互换后的字符串

    示例:

    s = 'HELLO world'
    s = s.swapcase()
    print(type(s),s)
    

    输出结果:

    <class 'str'> hello WORLD
    View Code

    22、title()

    描述:将字符串中的所有单词的第一个首字母大写

    参数:  

    返回值:返回所有单词首字母大写后的字符串

    示例:

    s = 'my name is xiaobai'
    s = s.title()
    print(type(s),s)
    

    输出结果:

    <class 'str'> My Name Is Xiaobai
    View Code
  • 相关阅读:
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第7节 内部类_7_内部类的概念与分类
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第6节 权限修饰符_6_四种权限修饰符
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第5节 final关键字_5_final关键字用于修饰成员变量
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第5节 final关键字_4_final关键字用于修饰局部变量
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第5节 final关键字_3_final关键字用于修饰成员方法
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第5节 final关键字_2_final关键字用于修饰类
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第5节 final关键字_1_final关键字概念与四种用法
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_24_笔记本USB接口案例_实现
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_23_笔记本USB接口案例_分析
    阶段1 语言基础+高级_1-3-Java语言高级_02-继承与多态_第4节 多态_22_用instanceof关键字进行
  • 原文地址:https://www.cnblogs.com/xiaoxiaobai/p/7612042.html
Copyright © 2011-2022 走看看