TheDeveloperBlog.com

Home | Contact Us

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

Python String splitlines() method with Examples

Python String splitlines() 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 splitlines() Method

Python splitlines() method splits the string based on the lines. It breaks the string at line boundaries and returns a list of splitted strings. Line breakers can be a new line (\n), carriage return (\r) etc. A table of line breakers are given below which split the string.

This method splits on the given line boundaries.

Representation Description
\n Line Feed
\r Carriage Return
\r\n Carriage Return + Line Feed
\v or \x0b Line Tabulation
\f or \x0c Form Feed
\x1c File Separator
\x1d Group Separator
\x1e Record Separator
\x85 Next Line (C1 Control Code)
\u2028 Line Separator
\u2029 Paragraph Separator

Signature

splitlines([keepends])

Parameters

keepends: It is a boolean value which can be either True or False. It is optional.

Return

It returns a comma separated list of lines.

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

Python String splitlines() Method Example 1

# Python splitlines() method example
# Variable declaration
str = "Java is a programming language"
# Calling function
str2 = str.splitlines() # returns a list having single element
print(str)
print(str2)
str = "Java \n is a programming \r language"
str2 = str.splitlines() # returns a list having splitted elements
print(str2)

Output:

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

Python String splitlines() Method Example 2

Passing True to the method which causes to include line breakers into the list of string. See the example below.

# Python splitlines() method example
# Variable declaration
str = "Java \n is a programming \r language"
# Calling function
str2 = str.splitlines(True) # returns a list having splitted elements
print(str2)

Output:

['Java \n', ' is a programming \r', ' language']

Python String splitlines() Method Example 3

# Python splitlines() method example
# Variable declaration
str = "Java \n is a programming \r language for \r\n  software development"
# Calling function
str2 = str.splitlines() # returns a list having splitted elements
# Displaying result
print(str2)
# getting back list to string
print("".join(str2)) # now it does not contain any line breaker character

Output:

['Java ', ' is a programming ', ' language for ', '  software development']
Java  is a programming  language for   software development

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