To separate a string by a specified separator in Python, you can use the split() method on the string. This method returns a list of substrings that were separated by the specified separator.
Here's an example:
main.py94 chars7 lines
Output:
main.py19 chars2 lines
In the example above, the string "hello,world" is split using the separator ",", resulting in a list ["hello", "world"].
You can also split a string multiple times by calling split() multiple times or by specifying the maxsplit parameter.
main.py129 chars8 lines
Output:
main.py34 chars2 lines
In the example above, the string "one,two,three,four,five" is split using the separator ",", only the first 2 occurrences, resulting in a list ["one", "two", "three,four,five"].
gistlibby LogSnag