python字符串从入门到精通

爱被打了一巴掌 2023-09-30 20:31 122阅读 0赞

5956f33c3bf941d3ac833aad9b5498ec.jpeg

1:capitalize()——->将字符串的第一个字符转换为大写

  1. str1 = "my name is michael"
  2. print(str1.capitalize())
  3. 运行结果:My name is michael

2:center(width, fillchar)——->返回一个指定的宽度 width 居中的字符串,fillchar 为填充的字符,默认为空格。

  1. str1 = "michael"
  2. print(str1.center(30, "*"))
  3. 运行结果:***********michael************

3:返回 str 在 string 里面出现的次数,如果 beg 或者 end 指定则返回指定范围内 str 出现的次数

  1. #不指定范围
  2. str1 = "michael jason"
  3. print("a的数量是: " , str1.count("a"))
  4. 运行结果:a的数量是: 2
  5. #指定范围
  6. str1 = "michael jason"
  7. print("a的数量是: " , str1.count("a", 0, 6))
  8. 运行结果:a的数量是: 1

4:检查字符串是否以 obj 结束,如果beg 或者 end 指定则检查指定的范围内是否以 obj 结束,如果是,返回 True,否则返回 False.

  1. str1 = "michael jason"
  2. print(str1.endswith("on"))
  3. 运行结果:True

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

  1. #找到字符返回True
  2. str1 = "michael jason"
  3. print(str1.find("i"))
  4. 运行结果:1
  5. #找不到返回-1
  6. str1 = "michael jason"
  7. 运行结果:print(str1.find("i", 2))

6: index(str, beg=0, end=len(string))——>跟find()方法一样,只不过如果str不在字符串中会报一个异常. 10isalnum()

  1. str1 = "michael jason"
  2. str2 = "jas"
  3. print(str1.index(str2))
  4. 运行结果:8

7:如果字符串至少有一个字符并且所有字符都是字母或数字则返 回 True,否则返回 False

  1. #不包含空格
  2. str1 = "runoob2016"
  3. print(str1.isalnum())
  4. 运行结果:True
  5. #包含空格
  6. str1 = "michael jason runoob2016"
  7. print(str1.isalnum())
  8. 运行结果:False

8:isalpha()——>如果字符串至少有一个字符并且所有字符都是字母则返回 True, 否则返回 False

  1. #不包含数字
  2. str1 = "michael"
  3. print(str1.isalpha())
  4. 运行结果:True
  5. #包含数字
  6. str1 = "michael001"
  7. print(str1.isalpha())
  8. 运行结果:False

9:isdigit()——>如果字符串只包含数字则返回 True 否则返回 False..

  1. #包含字符
  2. str1 = "michael001"
  3. print(str1.isdigit())
  4. 运行结果:False
  5. #只包含数字
  6. str1 = "1789456001"
  7. print(str1.isdigit())
  8. 运行结果:True

10:islower()——>如果字符串中包含至少一个区分大小写的字符,并且所有这些(区分大小写的)字符都是小写,则返回 True,否则返回 False

  1. #包含大写字母
  2. str1 = "Michael"
  3. print(str1.islower())
  4. 运行结果:False
  5. #包含大写字母
  6. str1 = "michael"
  7. print(str1.islower())
  8. 运行结果:True

11:join(seq)——>以指定字符串作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串

  1. seq = ["M", "y", "n", "a", "m", "e"]
  2. print("".join(seq))
  3. 运行结果:Myname

12:split(str=””, num=string.count(str)), num=string.count(str)) 以 str 为分隔符截取字符串,如果 num 有指定值,则仅截取 num+1 个子字符串

  1. str = "this is string example....wow!!!"
  2. print(str.split())
  3. print(str.split("i", 1))
  4. print(str.split("w"))
  5. 运行结果:
  6. ['this', 'is', 'string', 'example....wow!!!']
  7. ['th', 's is string example....wow!!!']
  8. ['this is string example....', 'o', '!!!']

13:splitlines([keepends])——-> splitlines() 按照行(‘\r’, ‘\r\n’, \n’)分隔,返回一个包含各行作为元素的列表,如果参数 keepends 为 False,不包含换行符,如果为 True,则保留换行符。

  1. str = "ab c\n\nde fg\rkl\r\n"
  2. print(str.splitlines())
  3. 运行结果:['ab c', '', 'de fg', 'kl']
  4. str = "ab c\n\nde fg\rkl\r\n"
  5. print(str.splitlines(True))
  6. 运行结果:['ab c\n', '\n', 'de fg\r', 'kl\r\n']

14:strip() 方法用于移除字符串头尾指定的字符(默认为空格)或字符序列。

注意:该方法只能删除开头或是结尾的字符,不能删除中间部分的字符。

  1. str = "*****this is **string** example....wow!!!*****"
  2. print(str.strip("*"))
  3. 运行结果:this is **string** example....wow!!!
  4. str = "123abcrunoob321"
  5. print(str.strip("12"))
  6. 运行结果:3abcrunoob3
  7. 注意删除多个字符时:只要头尾有对应其中的某个字符即删除,不考虑顺序,直到遇到第一个不包含在其中的字符为止。
  8. str = '123132231213321312==321312213231123132'
  9. print(str.strip('123'))
  10. 运行结果:==

15:返回长度为 width 的字符串,原字符串右对齐,前面填充0

  1. str = "Michael"
  2. print(str)
  3. print(str.zfill(20))
  4. print(str.rjust(20, "*"))
  5. 运行结果:
  6. Michael
  7. 0000000000000Michael
  8. *************Michael

发表评论

表情:
评论列表 (有 0 条评论,122人围观)

还没有评论,来说两句吧...

相关阅读

    相关 Python数组队列:入门精通

    Python数组队列:从入门到精通 Python语言是一种高级编程语言,它在数据结构方面有着非常强大的支持,其中最常用的数据结构之一就是数组和队列。本文将深入讲解Python

    相关 Python入门精通_前言

    前言 学而时习之,不亦乐乎 最近重新刷了一遍七月在线的《Python从入门到精通》的基础课程,再次系统的打一下Python基础。我把课程内容整理成 .ipynb 和 .