How to Split a String with Python

0 min read 160 words

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)
Tags:
Andrew
Andrew

Andrew is a visionary software engineer and DevOps expert with a proven track record of delivering cutting-edge solutions that drive innovation at Ataiva.com. As a leader on numerous high-profile projects, Andrew brings his exceptional technical expertise and collaborative leadership skills to the table, fostering a culture of agility and excellence within the team. With a passion for architecting scalable systems, automating workflows, and empowering teams, Andrew is a sought-after authority in the field of software development and DevOps.

Tags