笨鸟编程-零基础入门Pyhton教程

 找回密码
 立即注册
查看: 3461|回复: 0

Python 字符串方法说明

[复制链接]

新手上路

Rank: 1

积分
3
发表于 2022-3-8 12:23:53 | 显示全部楼层 |阅读模式
字符串方法capitalize()
  • 方法返回一个字符串,其中第一个字符是大写的
  • 其余的都是小写的。
  1. txt = "hello world"

  2. x = txt.capitalize()

  3. # output
  4. Hello world
复制代码





casefold()
  • 方法返回一个字符串,其中所有字符都是小写的。
  • 此方法类似于 lower() 方法,但 casefold() 方法更强,更具侵略性,这意味着它将更多字符转换为小写
  • 并且在比较两个字符串时会找到更多匹配项,并且两者都使用 casefold() 方法进行转换。
  1. txt = "Hello, And Welcome!"

  2. x = txt.casefold()

  3. # output
  4. hello, and welcome!
复制代码





center()
  • 方法将字符串居中对齐,使用指定的字符(空格为默认值)作为填充字符。
  1. txt = "welcome"

  2. x = txt.center(20, "*")

  3. # output
  4.    welcome  
  5. ******welcome*******
复制代码



count()
  • 方法返回指定值在字符串中出现的次数。
  1. txt = "hello, and welcome and hi! "

  2. x = txt.count("and")

  3. # output
  4. 2
复制代码




endwith()
  • 如果字符串以指定的值结尾,则方法返回 True,否则为 False。
  1. txt = "hello world!"

  2. x = txt.endswith("!")

  3. # output
  4. True
复制代码





endswith()
  • 方法查找指定值的第一个匹配项。
  • 方法返回 -1 如果找不到该值。
  • 注意:
  • find() 方法与 index() 方法几乎相同,
  • 唯一的区别是,如果未找到值,index() 方法将引发异常。
  1. txt = "Hello, welcome to python string."

  2. x = txt.find('welcome')

  3. # output
  4. 7
复制代码





format()
  • 方法设置指定值的格式,并将它们插入字符串的占位符内。
  • 占位符使用大括号定义:{}。
  1. txt1 = "Hello, {} to python {}".format("welcome", "string")

  2. txt2 = "Hello, I'm {name} and I'm a {job}".format(name="Mahmoud", job="developer")

  3. txt3 = "My name is {0}, I'm {1}".format("John",36)

  4. # output
  5. 1 - Hello, welcome to python string
  6. 2 - Hello, I'm Mahmoud and I'm a developer
  7. 3 - My name is John, I'm 36
复制代码




format_map()
  • 方法是Python中的内置函数。
  • 用于返回字典键的值。
  1. details = {
  2.     "name": "John",
  3.     "job": "developer"
  4. }

  5. x = "Hi, I'm {name} and I'm a {job}. ".format_map(details)

  6. # output
  7. Hi, I'm John and I'm a developer.
复制代码





index()
  • 方法查找指定值的第一个匹配项。
  • 如果未找到该值,则 index() 方法将引发异常。
  • 注意:
  • index() 方法几乎与 find() 方法相同
  • 唯一的区别是,如果未找到值,find() 方法将返回 -1。

  1. txt = "hello world!"

  2. x = txt.index("!")

  3. # output
  4. 11
复制代码




isalnum()
= 方法返回 True,如果所有字符都是字母数字。
  • 表示字母字母 (a-z) 和数字 (0-9)。
  1. txt = "Company12"

  2. x = txt.isalnum()

  3. # output
  4. True
复制代码





isalpha()
  • 如果所有字符都是字母 (a-z),则方法返回 True。
  • 不是字母的字符示例:(空格)!#%&?等。
  1. txt = "helloworld"

  2. x = txt.isalpha()

  3. # output
  4. True
复制代码




isdigit()
  • 如果所有字符都是数字,则方法返回 True,否则返回 False。
  1. txt = "2022"

  2. x = txt.isdigit()

  3. # output
  4. True
复制代码




isidentifier()
  • 如果字符串是有效的标识符,则方法返回 True,否则返回 False。
  • 如果字符串仅包含字母数字字母 (a-z) 和 (0-9) 或下划线 (_),则将其视为有效标识符。
  • 有效的标识符不能以数字开头,也不能包含任何空格。
  1. txt = "Python"

  2. x = txt.isidentifier()

  3. # output
  4. True
复制代码




islower()
  • 如果所有字符都为小写,则方法返回 True,否则为 False。
  • 不检查数字、符号和空格,只检查字母字符。
  1. txt = "hello world"

  2. x = txt.islower()

  3. # output
  4. True
复制代码




lstrip()
  • 方法删除任何前导字符(空格是要删除的默认前导字符)
  1. txt = "     Python      "

  2. x = txt.lstrip()

  3. # output
  4. Welcome to Python       methods explanation
复制代码




maketrans()
  • 方法返回一个映射表,该映射表可与 translate() 方法一起使用以替换指定的字符。
  1. txt = "Delcome to Python"

  2. trans = txt.maketrans("D", "W")

  3. result = txt.translate(trans)

  4. # output
  5. Welcome to Python
复制代码





partition()
  • 方法搜索指定的字符串,并将该字符串拆分为包含三个元素的元组。
    1- 第一个元素包含指定字符串前面的部分。
    2- 第二个元素包含指定的字符串。
    3- 第三个元素包含字符串后面的部分。
  • 注意:此方法搜索指定字符串的第一个匹配项。

  1. txt = "welcome to python methods explanation"

  2. x = txt.partition("python")

  3. # output
  4. ('welcome to ', 'python', ' methods explanation')
复制代码





replace()
  • 方法将指定的短语替换为另一个指定的短语。
  • 注意:指定短语的所有匹配项都将被替换
  • 如果未指定任何其他内容。
  1. txt = "welcome to python method explanation"

  2. x = txt.replace("python", "replace()")

  3. # output
  4. welcome to replace() method explanation
复制代码




rfind()
  • 方法查找指定值的最后一次匹配项。
  • 方法返回 -1 如果找不到该值。
  1. txt = "welcome to python method explanation"

  2. x = txt.rfind("explanation")

  3. # output
  4. 25
复制代码




rjust()
  • 方法将右对齐字符串,使用指定的字符(空格是默认值)作为填充字符。
  1. txt = "hello world"

  2. x = txt.rjust(20)

  3. # output
  4.           hello world
复制代码





split()
  • 方法将字符串拆分为一个列表。
  • 您可以指定分隔符,默认分隔符是任意空格。
  1. txt = "hello world"

  2. x = txt.split()

  3. # output
  4. ['hello', 'world']
复制代码





splitlines()
  • 方法将字符串拆分为一个列表。拆分在换行符处完成。
  1. txt = "welcome to python\n method explanation"

  2. x = txt.splitlines()

  3. # output
  4. ['welcome to python', ' method explanation']
复制代码




startswith()
  • 如果字符串以指定的值开头,则方法返回 True,否则为 False。
  1. txt = "Hello world"

  2. x = txt.startswith("Hello")

  3. # output
  4. True
复制代码





strip()
  • 方法删除任何前导(开头的空格)和尾随
  • (末尾的空格)字符(空格是要删除的默认前导字符)
  1. txt = "    python      "

  2. x = txt.strip()

  3. # output
  4. welcome to python method explanation
复制代码





swapcase()
  • 方法返回一个字符串,其中所有大写字母都是小写字母,反之亦然。
  1. txt = "Hello My Name Is MAHMOUD"

  2. x = txt.swapcase()

  3. # output
  4. hELLO mY nAME iS mahmoud
复制代码





title()
  • 方法返回一个字符串,其中每个单词中的第一个字符都是大写的。如标题或标题。
  • 如果单词包含数字或符号,则之后的第一个字母将转换为大写。
  1. txt = "python method explanation"

  2. x = txt.title()

  3. # output
  4. Python Method Explanation
复制代码





upper()
  • 方法返回一个字符串,其中所有字符都为大写。
  • 符号和数字将被忽略。
  1. txt = "Hello my friends"

  2. x = txt.upper()


  3. # output
  4. HELLO MY FRIENDS
复制代码




zfill()
  • 方法在字符串的开头添加零 (0),直到它达到指定的长度。
  • 如果 len 参数的值小于字符串的长度,则不执行填充。
  1. txt = "50"

  2. x = txt.zfill(10)

  3. # output
  4. 0000000050
复制代码




注意:
  • 所有字符串方法都返回新值。
  • 它们不会更改原始字符串。
祝您一切顺利。

回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|笨鸟自学网 ( 粤ICP备20019910号 )

GMT+8, 2024-9-8 11:12 , Processed in 0.019583 second(s), 19 queries .

© 2001-2020

快速回复 返回顶部 返回列表