
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
- Python Advanced Tutorial
- Python - Classes/Objects
- Python - Reg Expressions
- Python - CGI Programming
- Python - Database Access
- Python - Networking
- Python - Sending Email
- Python - Multithreading
- Python - XML Processing
- Python - GUI Programming
- Python - Further Extensions
- Python Useful Resources
- Python - Questions and Answers
- Python - Quick Guide
- Python - Tools/Utilities
- Python - Useful Resources
- Python - Discussion
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python String splitlines() Method
The Python String splitlines() method breaks a string at line boundaries. These line boundaries are a part of Universal newlines. Various line boundaries this method recognizes are given below:
\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
Note – These line breaks will be not displayed in the output unless specified.
Syntax
Following is the syntax for Python String splitlines() method −
str.splitlines()
Parameters
Keepends − This is an optional parameter, if its value as true, line breaks need are also included in the output.
Return Value
This method returns a list with all the lines in string, optionally including the line breaks (if num is supplied and is true).
Example
When the method is called on the string containing the line breaks, the output is returned as the split string.
The following example shows the usage of Python String splitlines() method.
str1 = "Names:\nAlex\nJohn\nRichard\nNick" print("String before splitting: " + str1) print("String after splitting:") print(str1.splitlines()) str2 = "Names:\rAlex\rJohn\rRichard\rNick" print("String before splitting: " + str2) print("String after splitting:") print(str2.splitlines())
When we run above program, it produces following result −
String before splitting: Names: Alex John Richard Nick String after splitting: ['Names:', 'Alex', 'John', 'Richard', 'Nick'] String before splitting: Names: Alex John Richard Nick String after splitting: ['Names:', 'Alex', 'John', 'Richard', 'Nick']
Example
If you pass “True” as a parameter to this method this includes the line breaks in the output.
In the following example, we will call the splitlines() method on an input string and pass the value "true" as an argument.
str1 = "Names:\nAlex\nJohn\nRichard\nNick" print("String before splitting: " + str1) print("String after splitting:") print(str1.splitlines(True)) str2 = "Names:\rAlex\rJohn\rRichard\rNick" print("String before splitting: " + str2) print("String after splitting:") print(str2.splitlines(True))
When we run above program, it produces following result −
String before splitting: Names: Alex John Richard Nick String after splitting: ['Names:\n', 'Alex\n', 'John\n', 'Richard\n', 'Nick'] String before splitting: Names: Alex John Richard Nick String after splitting: ['Names:\r', 'Alex\r', 'John\r', 'Richard\r', 'Nick']