A string "a,b,c" — three items joined by commas. You want them as a list of three strings. What's the obvious operation?
Split on the comma.
Exactly the verb. split is a string method:
parts = "a,b,c".split(",")
print(parts) # ['a', 'b', 'c']The argument is the delimiter — the character (or substring) Python should split on. The delimiter itself is removed from the result.
What about whitespace?
split() with no arguments splits on any whitespace and collapses runs:
"hello world here".split() # ['hello', 'world', 'here']The most common case — splitting words from a sentence — needs no argument.
And what about multi-line text?
split("\n") works, but splitlines() is the dedicated method — handles \r\n (Windows line endings) and \n consistently. Use it any time you have multi-line text.
str.split() — break a string into a listThe inverse of joining strings with +. Given a string, produce a list of its parts:
"a,b,c".split(",") # ['a', 'b', 'c']
"one two three".split() # ['one', 'two', 'three']split(delimiter)With an argument, split on that exact substring:
"alice,30,nyc".split(",") # ['alice', '30', 'nyc']
"path/to/file.py".split("/") # ['path', 'to', 'file.py']
"foo--bar--baz".split("--") # ['foo', 'bar', 'baz'] — multi-char delimiterThe delimiter is removed; only the surrounding parts remain. Empty strings appear if delimiters are adjacent: "a,,b".split(",") is ['a', '', 'b'].
split() — no argumentSplits on any whitespace, and collapses runs of whitespace:
" hello world ".split() # ['hello', 'world']Leading and trailing whitespace are also stripped. The default behaviour is exactly what you want for word-by-word splitting.
splitlines() — split on line breaksFor multi-line text, prefer splitlines() over split("\n"):
text = "line one\nline two\nline three"
text.splitlines() # ['line one', 'line two', 'line three']It handles \r\n (Windows) and \n (Unix) consistently, and doesn't leave a trailing empty string when the text ends with a newline.
Strings are immutable. split doesn't modify the string; it returns a fresh list:
original = "a,b,c"
parts = original.split(",")
print(original) # 'a,b,c' — unchanged
print(parts) # ['a', 'b', 'c']"".join(list)",".join(["a", "b", "c"]) # 'a,b,c'split and join are inverses. The string you call .join on is the delimiter to glue with. Useful any time you have a list of strings and need a single string back.
A string "a,b,c" — three items joined by commas. You want them as a list of three strings. What's the obvious operation?
Split on the comma.
Exactly the verb. split is a string method:
parts = "a,b,c".split(",")
print(parts) # ['a', 'b', 'c']The argument is the delimiter — the character (or substring) Python should split on. The delimiter itself is removed from the result.
What about whitespace?
split() with no arguments splits on any whitespace and collapses runs:
"hello world here".split() # ['hello', 'world', 'here']The most common case — splitting words from a sentence — needs no argument.
And what about multi-line text?
split("\n") works, but splitlines() is the dedicated method — handles \r\n (Windows line endings) and \n consistently. Use it any time you have multi-line text.
str.split() — break a string into a listThe inverse of joining strings with +. Given a string, produce a list of its parts:
"a,b,c".split(",") # ['a', 'b', 'c']
"one two three".split() # ['one', 'two', 'three']split(delimiter)With an argument, split on that exact substring:
"alice,30,nyc".split(",") # ['alice', '30', 'nyc']
"path/to/file.py".split("/") # ['path', 'to', 'file.py']
"foo--bar--baz".split("--") # ['foo', 'bar', 'baz'] — multi-char delimiterThe delimiter is removed; only the surrounding parts remain. Empty strings appear if delimiters are adjacent: "a,,b".split(",") is ['a', '', 'b'].
split() — no argumentSplits on any whitespace, and collapses runs of whitespace:
" hello world ".split() # ['hello', 'world']Leading and trailing whitespace are also stripped. The default behaviour is exactly what you want for word-by-word splitting.
splitlines() — split on line breaksFor multi-line text, prefer splitlines() over split("\n"):
text = "line one\nline two\nline three"
text.splitlines() # ['line one', 'line two', 'line three']It handles \r\n (Windows) and \n (Unix) consistently, and doesn't leave a trailing empty string when the text ends with a newline.
Strings are immutable. split doesn't modify the string; it returns a fresh list:
original = "a,b,c"
parts = original.split(",")
print(original) # 'a,b,c' — unchanged
print(parts) # ['a', 'b', 'c']"".join(list)",".join(["a", "b", "c"]) # 'a,b,c'split and join are inverses. The string you call .join on is the delimiter to glue with. Useful any time you have a list of strings and need a single string back.
Create a free account to get started. Paid plans unlock all tracks.