Python强大的String字符串类型
字符串
文本是一种字符串数据类型。任何被写成文本的数据类型都是字符串。单引号、双引号或三引号包含的任何数据都是字符串。有不同的字符串方法和内置函数来处理字符串数据类型。要检查字符串的长度,请使用len()方法。
初始化一个字符串
letter = 'P' # 字符串A可以是单个字符或是单词或句子
print(letter) # P
print(len(letter)) # 1
greeting = 'Hello, World!' # 字符串可以用单引号或是双引号
print(greeting) # Hello, World!
print(len(greeting)) # 13
sentence = "I hope you are enjoying 30 days of Python Challenge"
print(sentence)
多行字符串可通过三个单引号(’’’)或三个双引号(""")创建。请参见下面的示例。
multiline_string = '''I am a teacher and enjoy teaching.
I didn't find anything as rewarding as empowering people.
That is why I created 30 days of python.'''
print(multiline_string)
# 也可以用这种方式来写
multiline_string = """I am a teacher and enjoy teaching.
I didn't find anything as rewarding as empowering people.
That is why I created 30 days of python."""
print(multiline_string)
连接字符串
我们可以将字符串连接在一起。将字符串合并或连接称为串联。请参见以下示例:
first_name = 'Asabeneh'
last_name = 'Yetayeh'
space = ' '
full_name = first_name + space + last_name
print(full_name) # Asabeneh Yetayeh
# 使用内置函数len()检查字符串的长度
print(len(first_name)) # 8
print(len(last_name)) # 7
print(len(first_name) > len(last_name)) # True
print(len(full_name)) # 16
字符串中的转义序列
在Python和其他编程语言中,\后面跟一个字符表示转义序列。让我们看看最常见的转义字符:
- \n:换行
- \t:制表符(相当于8个空格)
- \:反斜杠
- ‘: 单引号(’)
- “: 双引号(”)
现在,让我们看一下这些转义序列的用法及其示例。
print('I hope everyone is enjoying the Python Challenge.\\nAre you ?') # line break
print('Days\\tTopics\\tExercises') # 单词间添加制表符或四个空格
print('Day 1\\t3\\t5')
print('Day 2\\t3\\t5')
print('Day 3\\t3\\t5')
print('Day 4\\t3\\t5')
print('This is a backslash symbol (\\\\)') # 为了写一个反斜杠
print('In every programming language it starts with \\"Hello, World!\\"') # 为了在引用中添加双引号
# output
I hope every one is enjoying the Python Challenge.
Are you ?
Days Topics Exercises
Day 1 5 5
Day 2 6 20
Day 3 5 23
Day 4 1 35
This is a backslash symbol (\\)
In every programming language it starts with "Hello, World!"
字符串格式化
旧格式字符串格式化(%运算符)
在Python中,有许多格式化字符串的方法。在这部分,我们将介绍其中一些。"%“运算符用于格式化一组变量(包含在一个"元组"中,即一个固定大小的列表)和一个格式化字符串,格式化字符串包含普通文本和"参数格式化符号”,像"%s"、"%d"、"%f"、"%.保留小数位数"等特殊符号。
- %s - 字符串(或任何具有字符串表示的对象,如数字)
- %d - 整数
- %f - 浮点数
- “%.保留小数位数” - 保留固定精度的浮点数
# 字符串类型
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
formated_string = 'I am %s %s. I teach %s' %(first_name, last_name, language)
print(formated_string)
# 字符串和数字
radius = 10
pi = 3.14
area = pi * radius ** 2
formated_string = 'The area of circle with a radius %d is %.2f.' %(radius, area) # 2后面保留2位小数
python_libraries = ['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']
formated_string = 'The following are python libraries:%s' % (python_libraries)
print(formated_string) # "The following are python libraries:['Django', 'Flask', 'NumPy', 'Matplotlib','Pandas']"
新式字符串格式化(str.format)
这种格式化是在Python 3中引入的。
first_name = 'Asabeneh'
last_name = 'Yetayeh'
language = 'Python'
formated_string = 'I am {} {}. I teach {}'.format(first_name, last_name, language)
print(formated_string) # I am Asabeneh Yetayeh. I teach Python
a = 4
b = 3
print('{} + {} = {}'.format(a, b, a + b))
print('{} - {} = {}'.format(a, b, a - b))
print('{} * {} = {}'.format(a, b, a * b))
print('{} / {} = {:.2f}'.format(a, b, a / b)) # 限制到小数点后两位
print('{} % {} = {}'.format(a, b, a % b))
print('{} // {} = {}'.format(a, b, a // b))
print('{} ** {} = {}'.format(a, b, a ** b))
# 结果
4 + 3 = 7
4 - 3 = 1
4 * 3 = 12
4 / 3 = 1.33
4 % 3 = 1
4 // 3 = 1
4 ** 3 = 64
# 字符串和数字
radius = 10
pi = 3.14
area = pi * radius ** 2
formated_string = 'The area of a circle with a radius {} is {:.2f}.'.format(radius, area) # 小数点后两位
print(formated_string) # The area of a circle with a radius 10 is 314.00.
字符串内插 / f-Strings(Python 3.6+)
另一种新的字符串格式化方法是f字符串。字符串以f开始,我们可以将数据注入到它们的相应位置。
a = 4
b = 3
print(f'{a} + {b} = {a +b}')
print(f'{a} - {b} = {a - b}')
print(f'{a} * {b} = {a * b}')
print(f'{a} / {b} = {a / b:.2f}')
print(f'{a} % {b} = {a % b}')
print(f'{a} // {b} = {a // b}')
print(f'{a} ** {b} = {a ** b}')
Python字符串作为字符序列
Python字符串是字符序列,并与其他Python有序对象序列-列表和元组的基本存取方法相同。从字符串中提取单个字符(和任意序列中的单个成员)的最简单方法是将它们解包到相应的变量中。
解包字符
language = 'Python'
a,b,c,d,e,f = language # 解包序列字符到变量中
print(a) # P
print(b) # y
print(c) # t
print(d) # h
print(e) # o
print(f) # n
通过索引访问字符串中的字符
在编程中,计数从零开始。因此字符串的第一个字母位于零索引,字符串的最后一个字母是字符串长度减一。

language = 'Python'
first_letter = language[0]
print(first_letter) # P
second_letter = language[1]
print(second_letter) # y
last_index = len(language) - 1
last_letter = language[last_index]
print(last_letter) # n
如果我们想从右侧开始访问字符串,我们可以使用负索引。-1是最后一个索引。
language = 'Python'
last_letter = language[-1]
print(last_letter) # n
second_last = language[-2]
print(second_last) # o
分割 Python 字符串
在Python中,我们可以将字符串切片成子字符串。
language = 'Python'
first_three = language[0:3] # starts at zero index and up to 3 but not include 3
print(first_three) #Pyt
last_three = language[3:6]
print(last_three) # hon
# 其他方式
last_three = language[-3:]
print(last_three) # hon
last_three = language[3:]
print(last_three) # hon
字符串反转
我们可以在Python中轻松反转字符串。
greeting = 'Hello, World!'
print(greeting[::-1]) # !dlroW ,olleH
分割字符串时跳过字符
在分割字符串时可以通过向分割字符串方法传递步骤参数来跳过字符。
language = 'Python'
pto = language[0:6:2] #
print(pto) # Pto
字符串方法
有许多字符串方法允许我们格式化字符串。请查看以下示例中的一些字符串方法:
capitalize()方法将字符串的第一个字符转换为大写字母。例如:
challenge = 'thirty days of python'
print(challenge.capitalize()) # 'Thirty days of python'
- count():返回字符串中子字符串的出现次数,count(子字符串,开始点,结束点)。开始是计数的起始索引,结束是最后一个要计数的索引。
challenge = 'thirty days of python'
print(challenge.count('y')) # 3
print(challenge.count('y', 7, 14)) # 1,
print(challenge.count('th')) # 2`
endswith()方法用于检查字符串是否以指定的后缀(子字符串)结尾。
challenge = 'thirty days of python'
print(challenge.endswith('on')) # True
print(challenge.endswith('tion')) # False
expandtabs()方法将字符串中的制表符替换为空格。默认情况下,一个制表符被替换为 8 个空格。你可以传入一个参数,指定需要将制表符替换为多少个空格。
challenge = 'thirty\\tdays\\tof\\tpython'
print(challenge.expandtabs()) # 'thirty days of python'
print(challenge.expandtabs(10)) # 'thirty days of python'
find()方法用于返回第一个匹配指定子字符串的索引位置。如果没有找到匹配的子字符串,则返回-1。
challenge = 'thirty days of python'
print(challenge.find('y')) # 16
print(challenge.find('th')) # 17
rfind():返回子字符串的最后一个出现的索引,如果未找到则返回-1。
challenge = 'thirty days of python'
print(challenge.rfind('y')) # 5
print(challenge.rfind('th')) # 1
index():返回子字符串的最低索引,附加参数指示起始和结束索引(默认为0和字符串长度-1)。如果未找到子字符串,则会引发ValueError。
challenge = 'thirty days of python'
sub_string = 'da'
print(challenge.index(sub_string)) # 7
print(challenge.index(sub_string, 9)) # error
rindex():返回子字符串的最高索引,附加参数指示起始和结束索引(默认为0和字符串长度-1)。
challenge = 'thirty days of python'
sub_string = 'da'
print(challenge.rindex(sub_string)) # 8
print(challenge.rindex(sub_string, 9)) # error
isalnum():检查是否为字母或数字字符。
challenge = 'ThirtyDaysPython'
print(challenge.isalnum()) # True
challenge = '30DaysPython'
print(challenge.isalnum()) # True
challenge = 'thirty days of python'
print(challenge.isalnum()) # False, space is not an alphanumeric character
challenge = 'thirty days of python 2019'
print(challenge.isalnum()) # False
isalpha():检查字符串元素是否为字母字符(a-z和A-Z)。
challenge = 'thirty days of python'
print(challenge.isalpha()) # False, space is once again excluded
challenge = 'ThirtyDaysPython'
print(challenge.isalpha()) # True
num = '123'
print(num.isalpha()) # False
isdecimal():检查字符串中所有字符是否为十进制数字符号(0-9)。
challenge = 'thirty days of python'
print(challenge.isdecimal()) # False
challenge = '123'
print(challenge.isdecimal()) # True
challenge = '\\u00B2'
print(challenge.isdigit()) # False
challenge = '12 3'
print(challenge.isdecimal()) # False, space not allowed
isdigit():检查字符串中所有字符是否为数字字符(0-9和某些其他表示数字的Unicode字符)。
challenge = 'Thirty'
print(challenge.isdigit()) # False
challenge = '30'
print(challenge.isdigit()) # True
challenge = '\\u00B2'
print(challenge.isdigit()) # True
isnumeric()函数的作用是检查一个字符串中的所有字符是否都是数字或与数字相关的字符(与isdigit()函数类似,但接受更多符号,例如½)
num = '10'
print(num.isnumeric()) # True
num = '\\u00BD' # ½
print(num.isnumeric()) # True
num = '10.5'
print(num.isnumeric()) # False
isidentifier()函数的作用是检查一个字符串是否是有效的标识符,即是否可以作为Python变量名使用。
challenge = '30DaysOfPython'
print(challenge.isidentifier()) # False, because it starts with a number
challenge = 'thirty_days_of_python'
print(challenge.isidentifier()) # True
islower()函数的作用是检查字符串中的所有字母是否都是小写字母。
challenge = 'thirty days of python'
print(challenge.islower()) # True
challenge = 'Thirty days of python'
print(challenge.islower()) # False
isupper(): 检查字符串中的所有字母字符是否都是大写的。
challenge = 'thirty days of python'
print(challenge.isupper()) # False
challenge = 'THIRTY DAYS OF PYTHON'
print(challenge.isupper()) # True
join(): 返回一个连结后的字符串。
web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
result = ' '.join(web_tech)
print(result) # 'HTML CSS JavaScript React'
web_tech = ['HTML', 'CSS', 'JavaScript', 'React']
result = '# '.join(web_tech)
print(result) # 'HTML# CSS# JavaScript# React'
strip(): 从字符串的开头和结尾移除所有给定的字符。
challenge = 'thirty days of pythoonnn'
print(challenge.strip('noth')) # 'irty days of py'
- replace(): 用给定的字符串替换子字符串。
challenge = 'thirty days of python'
print(challenge.replace('python', 'coding')) # 'thirty days of coding'
- split(): 使用给定的字符串或空格作为分隔符将字符串拆分为列表。
challenge = 'thirty days of python'
print(challenge.split()) # ['thirty', 'days', 'of', 'python']
challenge = 'thirty, days, of, python'
print(challenge.split(', ')) # ['thirty', 'days', 'of', 'python']
- title(): 返回首字母大写的字符串。
challenge = 'thirty days of python'
print(challenge.title()) # Thirty Days Of Python
- swapcase(): 将所有大写字母转换为小写字母,将所有小写字母转换为大写字母。
challenge = 'thirty days of python'
print(challenge.swapcase()) # THIRTY DAYS OF PYTHON
challenge = 'Thirty Days Of Python'
print(challenge.swapcase()) # tHIRTY dAYS oF pYTHON
- startswith(): 检查字符串是否以指定字符串开始。
challenge = 'thirty days of python'
print(challenge.startswith('thirty')) # True
challenge = '30 days of python'
print(challenge.startswith('thirty')) # False
评论区