- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Variable Types
- Python - Basic 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
Description
Python string method splitlines() returns a list with all the lines in string, optionally including the line breaks (if num is supplied and is true)
Syntax
Following is the syntax for 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.
Example
The following example shows the usage of splitlines() method.
#!/usr/bin/python str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"; print str.splitlines( ) print str.splitlines( 0 ) print str.splitlines( 3 ) print str.splitlines( 4 ) print str.splitlines( 5 )
When we run above program, it produces following result −
['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
Example
If you pass “True” as a parameter to this method this includes the line breaks in the output.
#!/usr/bin/python str = "Line1-a b c d e f\nLine2- a b c\n\nLine4- a b c d"; print str.splitlines(True) print str.splitlines( 0 ) print str.splitlines( 3 ) print str.splitlines( 4 ) print str.splitlines( 5 )
Output
When we run above program, it produces following result −
['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f', 'Line2- a b c', '', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d'] ['Line1-a b c d e f\n', 'Line2- a b c\n', '\n', 'Line4- a b c d']
python_strings.htm
Advertisements