TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

Python String split() method with Examples

Python String split() method with Examples on capitalize(), center(), count(), encode(), find(), format(), index(), join(), lower(), ljust(), isupper(), istitle(), isspace(), isprintable(), isnumeric(), islower() etc.

<< Back to PYTHON

Python String split() Method

Python split() method splits the string into a comma separated list. It separates string based on the separator delimiter. This method takes two parameters and both are optional. It is described below.

Signature

split(sep=None, maxsplit=-1)

Parameters

sep: A string parameter acts as a seperator.

maxsplit: Number of times split perfome.

Return

It returns a comma separated list.

Let's see some examples of split() method to understand it's functionality.

Python String split() Method Example 1

This is a simple example to understand the usage of split() method. No parameter is given, by default whitespaces work as separator. See the example below.

# Python split() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.split()
# Displaying result
print(str)
print(str2)

Output:

Java is a programming language
['Java', 'is', 'a', 'programming', 'language']

Python String split() Method Example 2

Let's pass a parameter separator to the method, now it will separate the string based on the separator. See the example below.

# Python split() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.split('Java')
# Displaying result
print(str2)3

Output:

['', ' is a programming language']

Python String rsplit() Method Example 3

The string is splited each time when a is occurred. See the example below.

# Python split() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.split('a')
# Displaying result
print(str)
print(str2)

Output:

Java is a programming language
['J', 'v', ' is ', ' progr', 'mming l', 'ngu', 'ge']

Python String split() Method Example 4

Along with separator, we can also pass maxsplit value. The maxsplit is used to set the number of times to split.

# Python split() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.split('a',1)
# Displaying result
print(str2)

str2 = str.split('a',3)
# Displaying result
print(str2)

Output:

['J', 'va is a programming language']
['J', 'v', ' is ', ' programming language']

Next TopicPython Strings




Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf