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)
Code language: Python (python)

Python Split String – Syntax

string.split(separator, maxsplit)
Code language: Python (python)

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)
Code language: Python (python)

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)
Code language: Python (python)

Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments