系列教程导航

  1. Python 基础教程
  2. 在 SublimeEditor 中配置 Python 环境
  3. Python 代码中添加注释
  4. Python 中的变量的使用
  5. Python 中的数据类型
  6. Python 中的关键字
  7. Python 字符串操作
  8. Python 中的 list 操作
  9. Python 中的 Tuple 操作
  10. Python max()和 min()–在列表或数组中查找最大值和最小值
  11. Python 找到最大的 N 个(前 N 个)或最小的 N 个项目
  12. Python 读写 CSV 文件
  13. Python 中使用 httplib2–HTTP GET 和 POST 示例
  14. Python 将 tuple 开箱为变量或参数
  15. Python 开箱 Tuple–太多值无法解压
  16. Python multidict 示例–将单个键映射到字典中的多个值
  17. Python OrderedDict–有序字典
  18. Python 字典交集–比较两个字典
  19. Python 优先级队列示例

Python 字符串基础

Python 中,字符串(string)具有以下特性:

  • 代表 Unicode 字符的字节数组
  • 使用单引号或双引号包裹
  • 长度无限(受内存限制)

字符串字面量

str = 'hello world'
str = "hello world"

多行字符串

多行字符串可以使用三个单引号或三个双引号创建:

str = '''Say hello

to python

programming'''

str = """Say hello

to python

programming"""
注意:Python 没有单独的字符数据类型,单个字符即为长度为 1 的字符串。

子字符串与切片 (Substring or Slicing)

通过使用切片(slice)语法,我们可以获取字符串的一部分。索引从零开始。

语法 str[m:n] 返回从位置 m(包含)到 n(不包含)的字符串。

示例:从索引 2 到 5 的子字符串

str = 'hello world'
print(str[2:5])  # 输出:llo

负切片

负索引将从末尾开始计数。

str = 'hello world'
print(str[-5:-2])  # 输出:wor
说明str[-m:-n] 返回从位置 -m(包含)到 -n(不包含)的字符串。

字符串作为数组 (Strings as Arrays)

在 Python 中,字符串表现为字符数组。方括号可用于访问字符串中的特定元素。

访问特定位置的字符

str = 'hello world'

print(str[0])   # 输出:h
print(str[1])   # 输出:e
print(str[2])   # 输出:l
# print(str[20])  # 若取消注释,将抛出 IndexError: string index out of range

字符串长度 (String Length)

len() 函数返回字符串的长度:

str = 'hello world'
print(len(str))  # 输出:11

字符串格式化 (String Formatting)

要在 Python 中格式化字符串,请在所需位置使用 {} 占位符,并将参数传递给 format() 函数。

可以在占位符中传递参数位置(从零开始)。

示例:使用 format()

age = 36
name = 'Lokesh'

txt = "My name is {} and my age is {}"
print(txt.format(name, age))    # 输出:My name is Lokesh and my age is 36

txt = "My age is {1} and the name is {0}"
print(txt.format(name, age))    # 输出:My age is 36 and the name is Lokesh

字符串方法 (String Methods)

6.1 capitalize()

返回一个字符串,其中第一个字符转换为大写。如果第一个字符为非字母,则返回原字符串。

name = 'lokesh gupta'
print(name.capitalize())  # 输出:Lokesh gupta

txt = '38 yrs old lokesh gupta'
print(txt.capitalize())   # 输出:38 yrs old lokesh gupta

6.2 casefold()

返回一个字符串,其中所有字符均转换为小写(比 lower() 更激进,用于大小写无关的比较)。

txt = 'My Name is Lokesh Gupta'
print(txt.casefold())  # 输出:my name is lokesh gupta

6.3 center()

使用指定的字符(默认为空格)作为填充,使字符串居中对齐。

txt = "hello world"
x = txt.center(20)
print(x)  # 输出:'    hello world     '

6.4 count()

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

  • count(value): 在整个字符串中搜索。
  • count(value, start, end): 在指定的起始和结束位置之间搜索。
txt = "hello world"
print(txt.count("o"))          # 输出:2
print(txt.count("o", 4, 7))    # 输出:1

6.5 encode()

使用指定的编码对字符串进行编码。如果未指定编码,默认使用 UTF-8。

txt = "My name is åmber"
x = txt.encode()
print(x)  # 输出:b'My name is \xc3\xa5mber'

6.6 endswith()

如果字符串以指定值结尾则返回 True,否则返回 False

txt = "hello world"
print(txt.endswith("world"))   # 输出:True
print(txt.endswith("planet"))  # 输出:False

6.7 expandtabs()

将制表符(\t)设置为指定的空格数。

txt = "hello\tworld"
print(txt.expandtabs(2))   # 输出:'hello world'
print(txt.expandtabs(4))   # 输出:'hello   world'
print(txt.expandtabs(16))  # 输出:'hello           world'

6.8 find()

查找指定值的第一次出现。如果未找到,返回 -1

注意find()index() 方法类似,区别在于 index() 在找不到值时会引发异常。
txt = "My name is Lokesh Gupta"
x = txt.find("e")
print(x)  # 输出:6

6.9 format()

格式化指定的字符串,并在占位符内插入参数值。

age = 36
name = 'Lokesh'
txt = "My name is {} and my age is {}"
print(txt.format(name, age))  # 输出:My name is Lokesh and my age is 36

6.10 format_map()

用于返回字典键的值,以格式化带有命名占位符的字符串。

params = {'name': 'Lokesh Gupta', 'age': '38'}
txt = "My name is {name} and age is {age}"
x = txt.format_map(params)
print(x)  # 输出:My name is Lokesh Gupta and age is 38

6.11 index()

  • 查找指定值在字符串中的第一次出现。
  • 如果找不到要搜索的值,则会引发异常。
txt = "My name is Lokesh Gupta"
x = txt.index("e")
print(x)  # 输出:6

# x = txt.index("z")  # 若取消注释,将抛出 ValueError: substring not found

6.12 isalnum()

检查字符串是否为字母数字。如果所有字符都是字母数字(a-zA-Z 和 0-9),则返回 True

print("LokeshGupta".isalnum())    # 输出:True
print("Lokesh Gupta".isalnum())   # 输出:False (包含空格)

6.13 isalpha()

如果所有字符都是字母(a-zA-Z),则返回 True

print("LokeshGupta".isalpha())      # 输出:True
print("Lokesh Gupta".isalpha())     # 输出:False (包含空格)
print("LokeshGupta38".isalpha())    # 输出:False (包含数字)

6.14 isdecimal()

如果所有字符均为十进制数字(0-9),则返回 True,否则返回 False

print("LokeshGupta".isdecimal())  # 输出:False
print("12345".isdecimal())        # 输出:True
print("123.45".isdecimal())       # 输出:False (包含小数点)
print("1234 5678".isdecimal())    # 输出:False (包含空格)

6.15 isdigit()

如果所有字符都是数字,则返回 True。指数也被认为是数字。

print("LokeshGupta".isdigit())      # 输出:False
print("12345".isdigit())            # 输出:True
print("123.45".isdigit())           # 输出:False (包含小数点)
print("1234\u00B2".isdigit())       # 输出:True (unicode 平方 2)

6.16 isidentifier()

如果字符串是有效的标识符,则返回 True

有效标识符规则:仅包含字母数字 (a-z, 0-9) 或下划线 (_);不能以数字开头;不能包含空格。
print("Lokesh_Gupta_38".isidentifier())   # 输出:True
print("38_Lokesh_Gupta".isidentifier())   # 输出:False (以数字开头)
print("_Lokesh_Gupta".isidentifier())     # 输出:True
print("Lokesh Gupta 38".isidentifier())   # 输出:False (包含空格)

6.17 islower()

如果所有字符均为小写,则返回 True。不检查数字、符号和空格,仅检查字母字符。

print("LokeshGupta".islower())         # 输出:False
print("lokeshgupta".islower())         # 输出:True
print("lokesh_gupta".islower())        # 输出:True
print("lokesh_gupta_38".islower())     # 输出:True

6.18 isnumeric()

如果所有字符都是数字(0-9),则返回 True。指数也被认为是数值。

print("LokeshGupta".isnumeric())    # 输出:False
print("12345".isnumeric())          # 输出:True
print("123.45".isnumeric())         # 输出:False (包含小数点)
print("1234\u00B2".isnumeric())     # 输出:True (unicode 平方 2)

6.19 isprintable()

如果所有字符都可打印,则返回 True,否则返回 False

不可打印字符示例:空白(被视为不可见的图形)、回车、制表符、换行、分页符、空字符。
print("LokeshGupta".isprintable())      # 输出:True
print("Lokesh Gupta".isprintable())     # 输出:True
print("Lokesh\tGupta".isprintable())    # 输出:False

6.20 isspace()

如果字符串中的所有字符都是空格,则返回 True,否则返回 False

print("   ".isspace())      # 输出:True
print("Hello".isspace())    # 输出:False

6.21 istitle()

如果文本的所有单词以大写字母开头,其余均为小写字母(即标题案例),则返回 True

print("Lokesh Gupta".istitle())  # 输出:True
print("Lokesh gupta".istitle())  # 输出:False

6.22 isupper()

如果所有字符均为大写,则返回 True。不检查数字、符号和空格,仅检查字母字符。

print("LOKESHGUPTA".isupper())   # 输出:True
print("LOKESH GUPTA".isupper())  # 输出:True
print("Lokesh Gupta".isupper())  # 输出:False

6.23 join()

以可迭代方式获取所有项目,并使用指定的分隔符将它们连接为一个字符串。

myTuple = ("Lokesh", "Gupta", "38")
x = "#".join(myTuple)
print(x)  # 输出:Lokesh#Gupta#38

6.24 ljust()

使用指定的字符(默认为空格)作为填充,使字符串左对齐。

txt = "lokesh"
x = txt.ljust(20, "-")
print(x)  # 输出:lokesh--------------

6.25 lower()

返回一个字符串,其中所有字符均为小写。符号和数字将被忽略。

txt = "Lokesh Gupta"
x = txt.lower()
print(x)  # 输出:lokesh gupta

6.26 lstrip()

删除所有前导字符(默认为空格)。

txt = "#Lokesh Gupta"
x = txt.lstrip("#_,.")
print(x)  # 输出:Lokesh Gupta

6.27 maketrans()

创建一个字符到其转换/替换的一对一映射。当在 translate() 方法中使用时,此映射用于将字符替换为映射的字符。

dict_map = {"a": "123", "b": "456", "c": "789"}
string = "abc"
print(string.maketrans(dict_map))  # 输出:{97: '123', 98: '456', 99: '789'}

6.28 partition()

在给定的文本中搜索指定的字符串,并将该字符串拆分为包含三个元素的元组:

  1. 指定字符串之前的部分。
  2. 指定的字符串本身。
  3. 指定字符串之后的部分。
txt = "my name is lokesh gupta"
x = txt.partition("lokesh")
print(x)      # 输出:('my name is ', 'lokesh', ' gupta')
print(x[0])   # 输出:my name is 
print(x[1])   # 输出:lokesh
print(x[2])   # 输出: gupta

6.29 replace()

将指定的短语替换为另一个指定的短语。

  • string.replace(oldvalue, newvalue)
  • string.replace(oldvalue, newvalue, count)count 指定要替换的出现次数,默认为所有。
txt = "A A A A A"
x = txt.replace("A", "B")
print(x)      # 输出:B B B B B

x = txt.replace("A", "B", 2)
print(x)      # 输出:B B A A A

6.30 rfind()

查找指定值的最后一次出现。如果未找到,返回 -1

txt = "my name is lokesh gupta"
x = txt.rfind("lokesh")    
print(x)        # 输出:11

x = txt.rfind("amit")      
print(x)        # 输出:-1

6.31 rindex()

查找指定值的最后一次出现。如果未找到,则引发异常。

txt = "my name is lokesh gupta"
x = txt.rindex("lokesh")       
print(x)                # 输出:11

# x = txt.rindex("amit")  # 若取消注释,将抛出 ValueError: substring not found

6.32 rjust()

使用指定的字符(默认为空格)作为填充,使字符串右对齐。

txt = "lokesh"
x = txt.rjust(20, "#")
print(x, "is my name")  # 输出:##############lokesh is my name

6.33 rpartition()

搜索指定字符串的最后一次出现,并将该字符串拆分为包含三个元素的元组(逻辑同 partition,但从右向左搜索)。

txt = "my name is lokesh gupta"
x = txt.rpartition("lokesh")
print(x)      # 输出:('my name is ', 'lokesh', ' gupta')
print(x[0])   # 输出:my name is 
print(x[1])   # 输出:lokesh
print(x[2])   # 输出: gupta

6.34 rsplit()

从右开始将字符串拆分为一个列表。

txt = "apple, banana, cherry"
x = txt.rsplit(", ")
print(x)  # 输出:['apple', 'banana', 'cherry']

6.35 rstrip()

删除所有结尾字符(字符串末尾的字符),空格是默认的结尾字符。

txt = "     lokesh     "
x = txt.rstrip()
print(x)  # 输出:'     lokesh'

6.36 split()

将字符串拆分为列表。您可以指定分隔符,默认分隔符为空格。

txt = "my name is lokesh"
x = txt.split()
print(x)  # 输出:['my', 'name', 'is', 'lokesh']

6.37 splitlines()

通过在换行符处进行拆分,将字符串拆分为列表。

txt = "my name\nis lokesh"
x = txt.splitlines()
print(x)  # 输出:['my name', 'is lokesh']

6.38 startswith()

如果字符串以指定值开头则返回 True,否则返回 False。字符串比较区分大小写。

txt = "my name is lokesh"
print(txt.startswith("my"))  # 输出:True
print(txt.startswith("My"))  # 输出:False

6.39 strip()

删除所有前导(开头)和结尾(末尾)字符,默认为空格。

txt = "   my name is lokesh   "
print(txt.strip())  # 输出:'my name is lokesh'

6.40 swapcase()

返回一个字符串,其中所有大写字母变为小写,反之亦然。

txt = "My Name Is Lokesh Gupta"
print(txt.swapcase())  # 输出:mY nAME iS lOKESH gUPTA

6.41 title()

返回一个字符串,其中每个单词的第一个字符均为大写。如果单词开头包含数字或符号,则其后的第一个字母将转换为大写字母。

print("lokesh gupta".title())      # 输出:Lokesh Gupta
print("38lokesh gupta".title())    # 输出:38Lokesh Gupta
print("1. lokesh gupta".title())   # 输出:1. Lokesh Gupta

6.42 translate()

需要转换表来根据映射表替换/翻译给定字符串中的字符。

translation = {97: None, 98: None, 99: 105}
string = "abcdef"  
print(string.translate(translation))  # 输出:idef

6.43 upper()

返回一个字符串,其中所有字符均为大写。符号和数字将被忽略。

txt = "lokesh gupta"
print(txt.upper())  # 输出:LOKESH GUPTA

6.44 zfill()

在字符串的开头添加零(0),直到达到指定的长度。

txt = "100"
x = txt.zfill(10)
print(x)  # 输出:0000000100

学习愉快!

说明:本文示例基于 Python 3 版本。部分方法行为在不同 Python 版本中可能略有差异,请以官方文档为准。