How to Split a String With Python


Splitting a string in Python is really easy.

You simply take a string and apply the split() method.

"Your String".split()

See some examples of the Python Split String method:

A Python Split String – Example

# Your string
a_string = "This is our string"

# Split into a list
a_list = a_string.split()

# Print the list
print(a_list)

Python Split String – Syntax

string.split(separator, maxsplit)

Parameter Values

ParameterDescription
separatorOptional – What to split the string on. The default is the whitespace character
maxsplitOptional – How many splits to perform. The default is -1, meaning “no limit”

Some examples for Python Split String

Use a comma (,) as the separator:

# Your string
a_string = "This, is, our, string"

# Split into a list
a_list = a_string.split(", ")

# Print the list
print(a_list)

Use a pipe (|) as the separator:

# Your string
a_string = "This|is|our|string"

# Split into a list
a_list = a_string.split("|")

# Print the list
print(a_list)