深度阅读

in python how to split a string

作者
作者
2023年08月22日
更新时间
9.21 分钟
阅读时间
0
阅读量

To split a string in Python, you can use the split() method. This method splits a string into a list of substrings based on a specified delimiter. Here’s an example:

my_string = "Hello, world!"
my_list = my_string.split(", ")
print(my_list)

In this example, the split() method is used to split the my_string variable into a list of substrings based on the comma and space delimiter. The resulting my_list will contain two strings: Hello and world!.

If no delimiter is specified, the split() method will split a string into a list of substrings using whitespace as the default delimiter. Here’s an example:

my_string = "Hello world!"
my_list = my_string.split()
print(my_list)

In this example, the split() method is used to split the my_string variable into a list of substrings based on whitespace. The resulting my_list will also contain two strings: Hello and world!.

博客作者

热爱技术,乐于分享,持续学习。专注于Web开发、系统架构设计和人工智能领域。