str.split()

Python · Strings

Theory

.split(sep) cuts a string at every occurrence of sep and returns a list of the pieces. 'Asha,IT,95000'.split(',') gives ['Asha', 'IT', '95000']. Notice the pieces are still strings: '95000' must go through int() before you can do maths with it. An optional maxsplit limits the cuts: 'a,b,c'.split(',', 1) gives ['a', 'b,c'].

Calling .split() with no argument is special: it splits on any run of whitespace (spaces, tabs, newlines) and ignores leading and trailing whitespace, so ' a b c'.split() gives ['a', 'b', 'c']. With an explicit separator every occurrence counts, so empty fields appear: 'a,,b'.split(',') gives ['a', '', 'b']. The reverse operation is join, called on the separator: ','.join(['a', 'b']) gives 'a,b'. For real CSV files with quoted commas, use the csv module.

Why we use it

Text often packs several values into one line: CSV rows, full names, log entries, tags. split() turns that line into a list you can index, loop over and convert field by field.

When to use it

Use split(',') for simple delimited lines, split() for words separated by spaces, and maxsplit when only the first separator matters. For CSV data with quotes or embedded commas, use the csv module or pandas.read_csv instead.

How it works

  1. Call line.split(',')
  2. Scan the string from left to right
  3. Reached a comma? Yes: End the current piece and start a new one. No: Add the character to the current piece.
  4. After the last character, close the final piece
  5. Return a list of strings: ['Asha', 'IT', '95000']

Syntax

string.split(separator)

Example

line = 'Asha,IT,95000'
print(line.split(','))

Key points

Python topics

Getting Started

Variables & Data Types

Operators

Strings

Lists

Tuples

Sets

Dictionaries

Control Flow

Loops

Functions & OOP

Working with Data

Tooling & Errors

File Handling

Exception Handling

NumPy Library

Pandas Basics

Data Cleaning

Combining DataFrames (SQL Joins & Unions)

GroupBy & SQL-Style Analytics

Reshaping & Workflow

Data Visualization

Machine Learning (Scikit-Learn)

Advanced Python & AI

T
Tech Town
Log InSign Up Free
Home SQL Python DSA Power BI Snowflake Databricks Spark Gen AI Practice Problems Contest Blog

Python for Data Analysts

From basics to business insights — the complete step-by-step Python roadmap. Click any topic in the sidebar to open it right here.

{{ sectionCount }}
Sections
{{ topicCount }}
Topics
Free
Forever
Hands-on practice Practice Python Online 141 Python questions for data analysts — strings, records, dates, statistics and regex — with hidden tests and instant verdicts. Start practising →
Section {{ sec.num }}

{{ sec.label }}

Python  /  {{ current.sectionLabel }}  /  {{ current.name }}

{{ current.name }}

Theory

{{ current.def }}

Why we use it

{{ rich.why }}

When to use it

{{ rich.when }}

Diagram

{{ rich.dg.caption }}

{{ tb.title }}
{{ c.v }}
{{ rich.dg.op }}
➜
{{ rich.dg.output.title }}
{{ c.v }}
{{ nd.label }}
{{ nd.sub }}
{{ nd.label }}
{{ nd.sub }}
{{ rich.dg.left.title }}
{{ it }}
{{ rich.dg.right.title }}
{{ it }}
Flowchart — how it works
{{ nd.text }}
{{ nd.text }}
◇ {{ nd.text }}
Yes → {{ nd.yes }}
No → {{ nd.no }}
Syntax
{{ current.syntax }}
Example
{{ current.example }}
▶ Try It Live — runs real Python 3.12 in your browser (first run loads the engine)
{{ liveOutput }}
{{ liveError }}
Key Points — click to check off as you learn
  • {{ pt.mark }} {{ pt.text }}

Share this topic