split a string into an array of substrings in python

To split a string into an array of substrings in Python, you can use the split() method.

Here's an example:

main.py
my_string = "Hello, world! How are you?"
my_array = my_string.split()
print(my_array)
86 chars
4 lines

This code will output ['Hello,', 'world!', 'How', 'are', 'you?'].

In this example, we used the split() method with no arguments, which splits the string at each whitespace character (spaces, tabs, and newlines). If you want to split the string at a different character or set of characters, you can pass the character(s) as an argument to split(). For example:

main.py
my_string = "1,2,3,4,5"
my_array = my_string.split(',')
print(my_array)
72 chars
4 lines

This code will output ['1', '2', '3', '4', '5'], with each comma-separated value as a separate array element.

gistlibby LogSnag