How to Split a String in Python

What you’ll build or solve

You’ll split a Python string into parts using split() and splitlines(), then clean the results for real input like CSV-style text or messy whitespace.

When this approach works best

This approach works best when you:

  • Need to turn a single line of text into a list, like splitting "a,b,c" into ["a", "b", "c"].
  • Parse user input, logs, or simple data formats where a known separator exists.
  • Break text into lines, like reading multiline content from a file or a textarea.

Avoid this approach when:

  • You need full CSV or JSON parsing with quotes, escapes, or nested structures. Use csv or json for that.

Prerequisites

  • Python installed
  • You know what a string and a list are

Step-by-step instructions

1) Split on whitespace with split()

If you call split() with no arguments, Python splits on any whitespace and ignores extra spaces and newlines.

text="  learn   python  faster\n today "
parts=text.split()
print(parts)

What to look for: This is the easiest way to split words from messy input because multiple spaces collapse automatically.


2) Split on a specific character

Pass a separator string to split(sep).

csv_line="python,java,go"
parts=csv_line.split(",")
print(parts)

You can split on multi-character separators too:

path="home->projects->mimo"
parts=path.split("->")
print(parts)

3) Limit the number of splits with maxsplit

Use maxsplit when you only want to split a few times, then keep the rest intact.

full_name="Ada Lovelace Byron"
first,rest=full_name.split(" ",maxsplit=1)
print(first)
print(rest)

This helps when a field can contain the separator later:

line="title:How to Split a String in Python"
key,value=line.split(":",maxsplit=1)
print(key,value)

4) Split into lines with splitlines()

Use splitlines() for text that contains line breaks. It handles \n, \r\n, and other newline types.

text="line one\nline two\r\nline three"
lines=text.splitlines()
print(lines)

If you want to keep the newline characters, pass True:

text="a\nb\n"
lines=text.splitlines(True)
print(lines)

What to look for: splitlines() is usually cleaner than split("\n") when text may come from different operating systems.


5) Clean up parts with strip() after splitting

When you split on a delimiter like a comma, you often get extra spaces around items. Use strip() to clean each piece.

tags="python,  data,  web "
parts= [p.strip()forpintags.split(",")]
print(parts)

Examples you can copy

Example 1: Split a sentence into words

sentence="Write code, test code, ship code."
words=sentence.split()
print(words)

Example 2: Parse a simple key=value setting

setting="theme=dark"
key,value=setting.split("=",maxsplit=1)
print(key,value)

Example 3: Turn a comma-separated list into clean values

raw="Mina, Sam, Ada, Zoe"
names= [name.strip()fornameinraw.split(",")]
print(names)

Example 4: Split multiline text into non-empty lines

This example filters out empty lines.

raw="first line\n\nsecond line\nthird line\n"
lines= [lineforlineinraw.splitlines()ifline.strip()]
print(lines)

Common mistakes and how to fix them

Mistake 1: Splitting on " " and getting empty strings

What you might do:

text="a  b   c"
parts=text.split(" ")
print(parts)

Why it breaks: Splitting on a single space treats each space as a separator, so multiple spaces create empty strings.

Correct approach: Use split() with no arguments.

text="a  b   c"
parts=text.split()
print(parts)

Mistake 2: Expecting split() to handle quoted CSV correctly

What you might do:

line='Ada,"Lovelace, Byron",34'
parts=line.split(",")
print(parts)

Why it breaks: Commas inside quotes should not split the field, but split() has no concept of quoting.

Correct approach: Use the csv module.

importcsv

line='Ada,"Lovelace, Byron",34'
parts=next(csv.reader([line]))
print(parts)

Troubleshooting

If you see empty strings in the result, do this: use split() with no arguments, or filter out empties with if part.

If your delimiter has spaces around it, do this: split first, then clean with strip() on each part.

If only the first separator should split, do this: use maxsplit=1.

If lines split incorrectly across operating systems, do this: use splitlines() instead of split("\n").

If your input uses quotes or escaping, do this: use csv or json instead of split().


Quick recap

  • Use split() with no arguments to split on any whitespace.
  • Use split(",") or another separator to split on a specific delimiter.
  • Use maxsplit to control how many splits happen.
  • Use splitlines() to split multiline text safely.
  • Use strip() on each piece to clean up spaces after splitting.