• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar
PythonForBeginners.com

PythonForBeginners.com

Learn By Example

  • Home
  • Learn Python
    • Python Tutorial
  • Categories
    • Basics
    • Lists
    • Dictionary
    • Code Snippets
    • Comments
    • Modules
    • API
    • Beautiful Soup
    • Cheatsheet
    • Games
    • Loops
  • Python Courses
    • Python 3 For Beginners
You are here: Home / Dictionary / How to use Split in Python

How to use Split in Python

Author: PFB Staff Writer
Last Updated: January 30, 2021

Learn how to use split in python.

Quick Example: How to use the split function in python

  1. Create an array

    x = ‘blue,red,green’

  2. Use the python split function and separator

    x.split(“,”) – the comma is used as a separator. This will split the string into a string array when it finds a comma.

  3. Result

    [‘blue’, ‘red’, ‘green’]

Definition

The split() method splits a string into a list using a user specified separator. When a separator isn’t defined, whitespace(” “) is used.

Why use the Split() Function?

At some point, you may need to break a large string down into smaller chunks, or strings. This is the opposite of concatenation which merges or combines strings into one.

To do this, you use the python split function. What it does is split or breakup a string and add the data to a string array using a defined separator.

If no separator is defined when you call upon the function, whitespace will be used by default. In simpler terms, the separator is a defined character that will be placed between each variable.

Examples of the Python Split Function In Action

Let’s take a look at some examples.

x = ‘blue,red,green’
x.split(“,”)
 
[‘blue’, ‘red’, ‘green’]
>>>
 
>>> a,b,c = x.split(“,”)
 
>>> a
‘blue’
 
>>> b 
‘red’
 
>>> c
‘green’

As you can see from this code, the function splits our original string which includes three colors and then stores each variable in a separate string. This leaves us with three strings of “a”, “b”, and “c”. Then, when you ask the interpreter to spit out the variables stored in these strings, you get the appropriate color.

Pretty neat, no? It’s also extremely useful when you’re working extensively with strings and variables.

Let’s look at another example.

>>> words = “This is random text we’re going to split apart”
 
>>> words2 = words.split(“ “)
 
>>> words2

[‘This’, ‘is’, ‘random’, ‘text’, ‘we’re’, ‘going’, ‘to’, ‘split’, ‘apart’]

What we did here is split the larger string and store the variables as a list under the “words2” string.

Related

Recommended Python Training

Course: Python 3 For Beginners

Over 15 hours of video content with guided instruction for beginners. Learn how to create real world applications and master the basics.

Enroll Now

Filed Under: Basics, Dictionary, Lists, Python Strings, Split Author: PFB Staff Writer

More Python Topics

API Argv Basics Beautiful Soup Cheatsheet Code Code Snippets Command Line Comments Concatenation crawler Data Structures Data Types deque Development Dictionary Dictionary Data Structure In Python Error Handling Exceptions Filehandling Files Functions Games GUI Json Lists Loops Mechanzie Modules Modules In Python Mysql OS pip Pyspark Python Python On The Web Python Strings Queue Requests Scraping Scripts Split Strings System & OS urllib2

Primary Sidebar

Menu

  • Basics
  • Cheatsheet
  • Code Snippets
  • Development
  • Dictionary
  • Error Handling
  • Lists
  • Loops
  • Modules
  • Scripts
  • Strings
  • System & OS
  • Web

Get Our Free Guide To Learning Python

Most Popular Content

  • Reading and Writing Files in Python
  • Python Dictionary – How To Create Dictionaries In Python
  • How to use Split in Python
  • Python String Concatenation and Formatting
  • List Comprehension in Python
  • How to Use sys.argv in Python?
  • How to use comments in Python
  • Try and Except in Python

Recent Posts

  • Count Rows With Null Values in PySpark
  • PySpark OrderBy One or Multiple Columns
  • Select Rows with Null values in PySpark
  • PySpark Count Distinct Values in One or Multiple Columns
  • PySpark Filter Rows in a DataFrame by Condition

Copyright © 2012–2025 · PythonForBeginners.com

  • Home
  • Contact Us
  • Privacy Policy
  • Write For Us