【Python 基础篇】Python字符串 之 进阶应用

字符串作为一种常见的数据类型,我们必须掌握一些常用的字符串处理函数。本文尽量囊括常用的字符串处理函数,此文也作为个人的小总结。

1.  find()

  • 功能:
    检测字符串是否包含特定字符(这里指的是字符,而不是字符串),如果包含,则返回开始的索引(返回匹配到的第一个索引位置,0,1,2,3,,,);否则,返回 -1。
  • 示例:
  • >>> str = "i love python"
    >>> str.find("love")
    2      // 返回第一个匹配字符的索引值
    >>> str.find("l")
    2      // 返回第一个匹配字符的索引值
    >>> str.find("0")
    -1     // 匹配失败,打印结果: -1
    >>> str.find("o")
    3      // 返回第一个匹配字符的索引值,第一次匹配到,即结束
    >>> str.find("i")
    0      // 返回第一个匹配字符的索引值
    >>> 

2.  index()

  • 功能:
    检测字符串是否包含指定字符
    (这里指的是字符,而不是字符串),如果包含,则返回开始的索引值;否则,提示错误。

  • 示例:

  • >>> str = "hi python ,let us go"
    >>> str.index("python")  // 返回第一个匹配的字符的索引值
    3
    >>> str.index("student")  //  匹配失败,则提示错误
    Traceback (most recent call last):
      File "<pyshell#36>", line 1, in <module>
        str.index("student")
    ValueError: substring not found
    >>> 

3.  count()

  • 功能:
    返回str1在string中指定索引范围内[start, end)出现的次数。

  • 示例:

  • >>> str1 = "hi,bro, today is first lession, we to learn python"
    >>> print(str1.count("o"))    // 统计整个字符串中出现的字符 “o” 的次数
    5
    >>> print(str1.count("to"))   // 统计出现字符串“to” 的次数
    2
    >>> print(str1.count("cpp"))    // 统计出现字符串“cpp” 字符串的次数
    0
    >>> print(str1.count("learn"))   // 统计出现字符串“learn” 字符串的次数
    1
    >>> print(str1.count("bro,"))   // 统计出现字符串“bro,” 字符串的次数
    1
    >>> 

4.  replace()

  • 语法:
    str1.replace(str2,str3,count)
  • 功能:
    将str1中的str2替换成str3,如果指定count,则不超过count次;
  • 示例:
  • >>> str1.replace("python",'systemverilog')
    'hi,bro, today is first lession, we to learn systemverilog'
    >>> str1.replace("bro",'dear friends ')
    'hi,dear friends , today is first lession, we to learn python'
    >>> 

5.  split()

  • 语法:
    str.split('分界符', maxSplit)
    maxSplit默认值为-1,表示根据定界符分割所有能分割的;
    返回值为列表;
  • 功能:
    如果 maxsplit有指定值,则仅分割 maxsplit 个子字符串;
  • 示例:
  • >>> str1.replace("python",'systemverilog')
    'hi,bro, today is first lession, we to learn systemverilog'
    >>> str1.replace("bro",'dear friends ')
    'hi,dear friends , today is first lession, we to learn python'
    >>> str1.replace(",","--")
    'hi--bro-- today is first lession-- we to learn python'
    >>> str1.replace("--","####")
    'hi,bro, today is first lession, we to learn python'
    >>> str1.replace(",","####")
    'hi####bro#### today is first lession#### we to learn python'
    >>> 

6.  capitalize()

  • 语法:
    str.capitalize()
    无参数;
  • 功能:
    将字符串的首字母大写,其余字母全部小写
  • 示例:
  • >>> print(str1)
    hi,bro, today is first lession, we to learn python
    >>> print(str1.capitalize())   //  将字符串的第一个字符,进行大写
    Hi,bro, today is first lession, we to learn python
    >>> 

7.  title()

  • 语法:
    str.title()
    无参数;
  • 功能:
    将字符串中的所有单词的首字母大写,其余字母全部小写;
    值得注意的是,这里单词的区分是以任何标点符号区分的,即,标点符号的前后都是一个独立的单词,字符串最后一个标点除外哦。详细看示例哈
  • 示例:
  • >>> print(str1.capitalize())
    Hi,bro, today is first lession, we to learn python
    >>> print(str1.title())
    Hi,Bro, Today Is First Lession, We To Learn Python
    >>> 

8.  startswith()/endswith()

  • 语法:
    str.startswith(str1)
  • 功能:
    检查字符串str是否 以字符串str1开头,若是,则返回True;否则,返回False;
  • 示例:
  • >>> print(str1.startswith("hi"))
    True
    >>> print(str1.endswith("python"))
    True

9.  lower()

  • 语法:
    str.lower()
    无参数;
  • 功能:
    将字符串的所有字母转换为小写;
  • 示例:

10.  upper()

  • 语法:
    str.upper()
    无参数;
  • 功能:
    将字符串的所有字母转换为大写;
  • 示例:

11.  ljust()

  • 语法:
    str.ljust(len)
  • 功能:
    将字符串左对齐,并使用空格填充至指定长度len;
  • 示例:

12.  rjust()

  • 语法:
    str.rjust(len)
  • 功能:
    将字符串右对齐,并使用空格填充至指定长度len;
  • 示例:

13.  center()

  • 语法:
    str.center(len)
  • 功能:
    将字符串居中,并使用空格填充至指定长度len;
  • 示例:

14.  lstrip()

  • 语法:
    str.lstrip()
  • 功能:
    去掉字符串左边的空白字符;
  • 示例:

15.  rstrip()

  • 语法:
    str.rstrip()
  • 功能:
    去掉字符串右边的空白字符;
  • 示例:

16.  strip()

  • 语法:
    str.strip()
  • 功能:
    去掉字符串左右两边的空白字符;
  • 示例:

17.  partition()

  • 语法:
    str.partition(str1)
  • 功能:
    根据str中的第一个str1,将字符串str分割为str1之前,str1和str1之后三个部分;若str1不存在,则将str作为第一部分,后面两个元素为空;返回元组;
  • 示例:

18.  join()

  • 语法:
    str.join(iterable)
  • 功能:
    将iterable中每两个相邻元素中间插入字符串str,返回形成的新的字符串;
  • 示例:

19.  isspace()

  • 语法:
    str.isspace()
    无参数;
  • 功能:
    如果字符串str中只包含空格,则返回True;否则,返回False;
  • 示例:

20.  isdigit()

  • 语法:
    str.isdigit()
    无参数;
  • 功能:
    如果字符串str中只包含数字,则返回True;否则,返回False;
  • 示例:
<span style="color:#000000"><span style="background-color:#f6f8fa"><code class="language-python"><span style="color:#880000 !important"><em>## isdigit()函数</em></span>
str16 = <span style="color:#009900 !important">"14250"</span>
print(str16.isdigit())
<span style="color:#880000 !important"><em>## 输出:</em></span>
<span style="color:#880000 !important"><em>##     True</em></span></code></span></span>

21.  isalpha()

  • 语法:
    str.isalpha()
    无参数;
  • 功能:
    如果字符串str中只包含字母,则返回True;否则,返回False;
  • 示例:
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值