Python split() Function: Syntax, Usage and Examples! (2024)

The string manipulation function in Python used to break down a bigger string into several smaller strings is called the split() function in Python. The split() function returns the strings as a list.

A Split() Function Can Be Used in Several Ways. They Are:

  • Splitting the string based on the delimiter space
  • Splitting the string based on the first occurrence of a character
  • Splitting the given file into a list
  • Splitting the string based on the delimiter new line character
  • Splitting the string based on the delimiter tab
  • Splitting the string based on the delimiter comma
  • Splitting the string based on multiple delimiters
  • Splitting the string into a list
  • Splitting the string based on the delimiter hash
  • Splitting the string by passing maxsplit parameter
  • Splitting the string into a character array
  • Splitting the string based on one of the substrings from the given string as the delimiter

Splitting the String Based on the Delimiter Space:

The given string or line is separated using the split() function with whitespace as the delimiter.

Example 1:

Python program to demonstrate split() function in Python with space as delimiter:

#creating a string variable to store the string to be split

string_to_be_split = 'We love Simplilearn'

#using split() function with space as delimiter to split the given string into smaller strings

print(string_to_be_split.split(" "))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (1)

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

Python split() Function: Syntax, Usage and Examples! (2)

Splitting the String Based on the First Occurrence of a Character:

The given string or line is separated using the split() function with the first occurrence of the character from the string specified as the delimiter.

Example 2:

Python program to demonstrate split() function in Python with the first occurrence of a given character in the string as delimiter:

string_to_be_split = 'Simplilearn'

#using split() function with the first occurrence of a given character in the string as delimiter to split the given string into smaller strings

print(string_to_be_split.split("i"))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (3)

Splitting the Given File Into a List:

The data in the file is split into several lines and each line is returned as an element in the list by making use of a split function called the splitlines() function in Python.

Example 3:

Python program to demonstrate splitlines() function in Python to split the data in the given file into a list:

#opening a file in read mode using open() function

fileopen = open("C:/Users/admin/Desktop/images/example.txt", "r")

#reading the contents of the file using read() function

fileread = fileopen.read()

#using splitlines() function to display the contents of the file as a list

print(fileread.splitlines())

fileopen.close()

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (4)

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

Python split() Function: Syntax, Usage and Examples! (5)

Splitting the String Based on the Delimiter New Line Character:

The given string or line is separated using a split() function with a new line character as the delimiter.

Example 4:

Python program to demonstrate split() function in Python with new line character as delimiter:

string_to_be_split = 'We\n love\n Simplilearn'

#using split() function with space as delimiter to split the given string into smaller strings

print(string_to_be_split.split("\n"))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (6)

Splitting the String Based on the Delimiter Tab:

The given string or line is separated using the split() function with a tab as the delimiter.

Example 5:

Python program to demonstrate split() function in Python with tab as the delimiter:

string_to_be_split = 'We\t love\t Simplilearn'

#using split() function with space as delimiter to split the given string into smaller strings

print(string_to_be_split.split("\t"))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (7)

Splitting the String Based on the Delimiter Comma:

The given string or line is separated using the split() function with a comma as the delimiter.

Example 6:

Python program to demonstrate split() function in Python with delimiter comma:

string_to_be_split = 'We, love, Simplilearn'

#using split() function with space as delimiter to split the given string into smaller strings

string_after_split = string_to_be_split.split(",")

print(string_after_split)

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (8)

Splitting the String Based on Multiple Delimiters:

Multiple delimiters can be specified as a parameter to the split() function by separating each delimiter with a |. The given string or line with multiple delimiters is separated using a split function called re.split() function.

Example 7:

Python program to demonstrate re.split() function in Python to split a given string or a line with multiple delimiters:

#importing the module re

import re

string_to_be_split = 'We, love\n Simplilearn'

#using re.split() function with comma and new line character as delimiters to split the given string into smaller strings

print(re.split(",|\n", string_to_be_split))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (9)

Splitting the String Into a List:

The given string or line can be split into a list using the split() function with any delimiters.

Example 8:

Python program to demonstrate split() function in Python to split a given string or a line with any delimiter:

string_to_be_split = 'We: love: Simplilearn'

#using split() function with : as delimiter to split the given string into smaller strings

print(string_to_be_split.split(":"))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (10)

Splitting the String Based on the Delimiter Hash:

The given string or line is separated using the split() function with a hash as the delimiter.

Example 9:

Python program to demonstrate split() function in Python to split a given string or a line with delimiter as hash:

string_to_be_split = 'We# love# Simplilearn'

#using split() function with # as delimiter to split the given string into smaller strings

print(string_to_be_split.split("#"))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (11)

Splitting the String by Passing the Maxsplit Parameter.

The maximum number of splits that a split() function can perform on a given string or line can be specified using the maxsplit parameter and passed as an argument to the split() function.

Example 10:

Python program to demonstrate split() function in Python with maxsplit parameter:

string_to_be_split = 'Welcome, to, Simplilearn'

#using split() function with maxsplit parameter to split the given string into smaller strings

print(string_to_be_split.split(" ", 2))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (12)

Splitting the String Into a Character Array:

The given string or line can be split into a list consisting of each character as the elements of the list using the split function called list() function.

Example 11:

Python program to demonstrate list() function in Python to split a given string or a line into several characters each one being the element in a list:

string_to_be_split = 'Simplilearn'

#using list() function to split the given string into a list

print(list(string_to_be_split))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (13)

Splitting the String Based on One of the Substrings From the Given String as the Delimiter:

The split() function can be used to split a given string or a line by specifying one of the substrings of the given string as the delimiter. The string before and after the substring specified as a delimiter is returned as the output.

Example 12:

Python program to demonstrate split() function in Python to split a given string or a line into several characters with one of the substrings of the given string being the delimiter:

string_to_be_split = 'Welcome, to, Simplilearn'

#using split() function with one of the substrings of the given string as the delimiter to split the given string into smaller strings

print(string_to_be_split.split("to"))

The output of the above program is shown in the snapshot below:

Python split() Function: Syntax, Usage and Examples! (14)

Want a Top Software Development Job? Start Here!

Full Stack Developer - MERN StackExplore Program

Python split() Function: Syntax, Usage and Examples! (15)

Python split() Function: Syntax, Usage and Examples! (2024)

FAQs

What is split () in Python with example? ›

The split() method is the most common way to split a string into a list in Python. This method splits a string into substrings based on a delimiter and returns a list of these substrings. In this example, we split the string "Hello world" into a list of two elements, "Hello" and "world" , using the split() method.

What is the syntax of split function? ›

Syntax. text - The text to divide. delimiter - The character or characters to use to split text . By default, each character in delimiter is considered individually, e.g. if delimiter is "the" , then text is divided around the characters "t" , "h" , and "e" .

Can split() take two arguments? ›

split() method accepts two arguments. The first optional argument is separator , which specifies what kind of separator to use for splitting the string. If this argument is not provided, the default value is any whitespace, meaning the string will split whenever . split() encounters any whitespace.

What does split() return if the string has no match in Python? ›

Python string split() function syntax

If the string contains consecutive delimiters, then an empty string is returned. Delimiter argument can be of multiple characters too. If the delimiter is not provided or None , then whitespaces are considered as the delimiter.

Why do we use split ()? ›

The Split function breaks a text string into a table of substrings. Use Split to break up comma delimited lists, dates that use a slash between date parts, and in other situations where a well defined delimiter is used. A separator string is used to break the text string apart.

What is the difference between split () and slicing in Python? ›

In essence: - `split()` divides a string into smaller parts based on a character or pattern. - Slicing extracts a portion of a string directly by specifying the start and end positions.

Does split () return a string? ›

The string manipulation function in Python used to break down a bigger string into several smaller strings is called the split() function in Python. The split() function returns the strings as a list.

How to split two characters in Python? ›

If you know the characters you want to split upon, just replace them with a space and then use split().

What to import to use split in Python? ›

For this, we need to make use of the re module of Python and import the re. split() function. In the above example, the re.

Is split by space or newline in Python? ›

The split() function in Python can be used not only to split strings but also to remove leading and trailing whitespace. When you call split() without passing any delimiter, it automatically splits the string at whitespace characters (spaces, tabs, and newlines) and discards any leading or trailing whitespace.

What is the opposite of string split Python? ›

s.join(list) -- opposite of split(), joins the elements in the given list together using the string as the delimiter.

What is the reverse of split function in Python? ›

The inverse of the split method is join . You choose a desired separator string, (often called the glue) and join the list with the glue between each of the elements.

What does * input () split () mean in Python? ›

input() returns a string typed in at the command line. . split() , applied to a string, returns a list consisting of whitespace-free substrings that had been separated by whitespace.

How to split lines in Python? ›

splitlines() is a built-in string method in Python that is used to split a multi-line string into a list of lines. It recognizes different newline characters such as \n , \r , or \r\n and splits the string at those points.

How to split a number in Python? ›

Step-by-step approach:
  1. Initialize the input list with a single integer.
  2. Convert the integer to a string using the str() function.
  3. Use a for loop to iterate over the characters of the string.
  4. Convert each character back to an integer using the int() function.
  5. Append the integer to the output list.
  6. Print the output list.
Apr 18, 2023

How to use split function in for loop in Python? ›

Split() method will split each word in a string into a list. If you don't specify the separator, the default separator is any whitespace in a string. str = "hello, my name is maria!" ['hello,', 'my', 'name', 'is', 'maria!

Top Articles
How to make passive income | Fidelity
What Is Fiat Money? | Bankrate
Durr Burger Inflatable
Dte Outage Map Woodhaven
Was ist ein Crawler? | Finde es jetzt raus! | OMT-Lexikon
Lamb Funeral Home Obituaries Columbus Ga
Lighthouse Diner Taylorsville Menu
Mail Healthcare Uiowa
Doby's Funeral Home Obituaries
Magic Mike's Last Dance Showtimes Near Marcus Cedar Creek Cinema
B67 Bus Time
Craigslist Dog Kennels For Sale
Wordscape 5832
Walthampatch
Gon Deer Forum
Kitty Piggy Ssbbw
Tnt Forum Activeboard
Craighead County Sheriff's Department
Craigslist Appomattox Va
Johnnie Walker Double Black Costco
Directions To Nearest T Mobile Store
2021 MTV Video Music Awards: See the Complete List of Nominees - E! Online
Sorrento Gourmet Pizza Goshen Photos
Synergy Grand Rapids Public Schools
Acurafinancialservices Com Home Page
Guinness World Record For Longest Imessage
Pay Stub Portal
Used Safari Condo Alto R1723 For Sale
Fastpitch Softball Pitching Tips for Beginners Part 1 | STACK
Have you seen this child? Caroline Victoria Teague
Royals op zondag - "Een advertentie voor Center Parcs" of wat moeten we denken van de laatste video van prinses Kate?
Umiami Sorority Rankings
拿到绿卡后一亩三分地
Leatherwall Ll Classifieds
Magicseaweed Capitola
Pawn Shop Open Now
Stanford Medicine scientists pinpoint COVID-19 virus’s entry and exit ports inside our noses
1v1.LOL Game [Unblocked] | Play Online
Kornerstone Funeral Tulia
Wayne State Academica Login
Pokemon Reborn Gyms
Isabella Duan Ahn Stanford
Fedex Passport Locations Near Me
Is Ameriprise A Pyramid Scheme
Uc Davis Tech Management Minor
How To Customise Mii QR Codes in Tomodachi Life?
Sc Pick 3 Past 30 Days Midday
Star Sessions Snapcamz
Affidea ExpressCare - Affidea Ireland
Latest Posts
Article information

Author: Dong Thiel

Last Updated:

Views: 6140

Rating: 4.9 / 5 (79 voted)

Reviews: 86% of readers found this page helpful

Author information

Name: Dong Thiel

Birthday: 2001-07-14

Address: 2865 Kasha Unions, West Corrinne, AK 05708-1071

Phone: +3512198379449

Job: Design Planner

Hobby: Graffiti, Foreign language learning, Gambling, Metalworking, Rowing, Sculling, Sewing

Introduction: My name is Dong Thiel, I am a brainy, happy, tasty, lively, splendid, talented, cooperative person who loves writing and wants to share my knowledge and understanding with you.