OneBite.Dev - Coding blog in a bite size

split a string in python

Code snippet on how to split a string in python

txt = "hello world, this is a string"

x = txt.split(", ")

print(x)

This code splits a string into a list of strings. It begins by defining a variable, txt, that stores the string to be split. We then use the built-in split() function to split the string on the parameter specified. In this case, the delimiter is a comma and space ”, “. Finally, we print the result, which is a list of the two split strings. The output of this code will be [“hello world”, “this is a string”].

python