Python Basics

How to split a string in python?

wondering how to split a string in python? Python has in-built function split that splits a string.

Split Function

By default python split function splits string by space and returns list of splitted string.

Example

str=”Once upon a time”

print(str.split(” “))

Output: [“Once”,”upon”,”a”,”time”]

Similarly we can pass any separator we want.

Example

str=”once-upon-a-time”

print(str.split(“-“))

Output: [“Once”,”upon”,”a”,”time”]