split a string by a delimiter in python
Code snippet on how to split a string by a delimiter in python
s = "This-is-a-string-with-delimiter"
result = s.split('-')
This code splits a string into multiple parts, separated by a delimiter. The delimiter in this case is a hyphen ’-‘.
The first line, s = "This-is-a-string-with-delimiter"
, creates a variable ‘s’ and assigns it the value of the string we want to split, ‘This-is-a-string-with-delimiter’.
The second line, result = s.split('-')
, uses the .split() method on the variable ‘s’ and passes the delimiter ’-’ to the method as an argument. This results in the variable ‘result’ being assigned a list of values, which are the parts of the original string split by the delimiter. The parts are: ‘This’, ‘is’, ‘a’, ‘string’, ‘with’, and ‘delimiter’.