Google News
logo
Python Program to Split a string into substrings
In the following example of Python program that demonstrates how to split a string into substrings using the built-in `split()` function :
Program :
# Split a string into substrings
my_string = "Hello, World!"
substrings = my_string.split(",")

print("The substrings are:", substrings)


In the above program, we start by assigning the string value `"Hello, World!"` to the variable `my_string`. We then use the `split()` function to split `my_string` into substrings at each occurrence of the comma `,`, and assign the resulting substrings to the variable `substrings`.

Finally, we use the `print()` function to print the message "The substrings are:" followed by the value of `substrings`.

When we run the program, it will output the following message :

Output :
The substrings are: ['Hello', ' World!']
As you can see, the `split()` function splits the string into substrings based on the delimiter `,`. The resulting substrings are stored in a list, which is assigned to the variable `substrings`.