3小时入门Python——第三十四课plit()和join()函数
在 Python 中,字符串和列表非常相似。首先,它们都属于序列,尽管字符串只限于字符,而列表可以存储不同类型的数据。此外,您可以遍历字符串和列表。但是,有时您需要将字符串转换为列表,反之亦然。Python 有这种工具。这将帮助你完成这个任务的方法是**split()
,join()
和 splitlines()
**。
分割字符串
该**split()
方法通过分隔符将字符串分成子字符串。如果未指定分隔符,则默认使用空格。该方法返回所有子字符串的列表**,并且值得注意的是,分隔符本身不包含在任何子字符串中。
# split example
definition = input() # 'Coin of the realm is the legal money of the country'
definition.split()
# ['Coin', 'of', 'the', 'realm', 'is', 'the', 'legal', 'money', 'of', 'the', 'country']
definition.split("legal")
# ['Coin of the realm is the ', ' money of the country']
您还可以使用**maxsplit
**分隔符后面的参数指定要进行多少次拆分。结果列表中的元素数将等于 maxsplit + 1
。
如果未指定参数,则会进行所有可能的分割。
# maxsplit example
definition = input() # 'Coin of the realm is the legal money of the country'
definition.split("of", 1)
# ['Coin ', ' the realm is the legal money of the country']
definition.split("of")
# ['Coin ', ' the realm is the legal money ', ' the country']
如果字符串中没有出现分隔符,则该方法的结果将是一个列表,其中原始字符串是其唯一元素:
definition = input() # 'Coin of the realm is the legal money of the country'
definition.split("hi!") # wrong separator
# ['Coin of the realm is the legal money of the country']
因此,在所有情况下都**split()
**允许我们将字符串转换为列表。
使用以下命令将输入直接读取到多个变量中可能也很有用 split()
:
name, surname = input().split() # Forrest Gump
print(name) # Forrest
print(surname) # Gump
当您知道输入值的确切数量时,这非常有效。如果您不这样做,则很可能会 ValueError
出现一条消息,告诉您要么有太多值需要解包,要么没有足够的值。所以记住这一点!
加入清单
该**join()
方法用于从字符串集合中创建一个字符串。但是,其使用有许多限制。首先,方法的参数必须是一个以字符串为元素的可迭代对象**。其次,该方法必须应用于分隔符:一个字符串,该字符串将分隔结果字符串对象中的元素。参见以下示例:
word_list = ["dog", "cat", "rabbit", "parrot"]
" ".join(word_list) # "dog cat rabbit parrot"
"".join(word_list) # "dogcatrabbitparrot"
"_".join(word_list) # "dog_cat_rabbit_parrot"
" and ".join(word_list) # "dog and cat and rabbit and parrot"
请注意,仅当可迭代对象中的元素是字符串时,此方法才有效。例如,如果您要创建一个整数字符串,则它将不起作用。在这种情况下,您需要将整数显式转换为字符串,或者仅从一开始就使用字符串。
int_list = [1, 2, 3]
" ".join(int_list) # TypeError!
str_list = ["1", "2", "3"]
" ".join(str_list) # "1 2 3"
分割多行
该**splitlines()
方法类似于 split()
,但是它专门用于按行边界分割字符串。有许多转义序列表示行的结尾,但是该 split()
方法只能采用一个分隔符。所以这是该 splitlines()
**方法派上用场的地方:
# splitlines example
long_text = 'first line\nsecond line\rthird line\r\nforth line'
long_text.splitlines()
# ['first line', 'second line', 'third line', 'forth line']
该方法具有一个可选参数**keepends
,其值为 True 或 False。如果 keepends = True
**结果列表中包含换行符:
# keepends
long_text = 'first line\nsecond line\rthird line\r\nforth line'
long_text.splitlines(keepends=True)
# ['first line\n', 'second line\r', 'third line\r\n', 'forth line']
您也可以一次使用多个字符串方法。这称为**链接,**它之所以有效,是因为大多数字符串方法都返回原始字符串的副本:
# chaining example
sent = input() # "Mary had a little lamb"
new_sent = sent.lower().split()
# ["mary", "had", "a", "little", "lamb"]
但是请不要气 carried,因为一行的长度不应超过 79 个字符,而且我们绝对不希望破坏 PEP 8!
结论
我们已经学习了如何通过 split()
和 splitlines()
方法将字符串转换为列表,以及如何通过 join()
方法从列表中获取字符串。作为回顾,请考虑以下几点:
- 拆分和联接方法不会更改原始字符串。
- 如果需要多次使用“更改的”字符串,则需要将相应方法的结果分配给变量。
- 如果只需要使用一次此结果,则可以当场使用它,例如,
print()
它。 - 字符串方法中有很多参数。如果需要微调程序,可以查看文档。
标题:3小时入门Python——第三十四课plit()和join()函数
作者:给我丶鼓励
地址:https://blog.doiduoyi.com/articles/1594214626713.html