How to Use String splitlines() Method in PythonPython Basics


The splitlines() method splits the string at line breaks and returns a list of lines in the string.

Example

#!/usr/bin/python3
# -*- coding: UTF-8 -*-

# Not keep line breaks after splitting
print('happynnbirthdaynto youn'.splitlines())

# Keep line breaks after splitting
print('happynnbirthdaynto youn'.splitlines(True)) 

Output:

['happy', '', 'birthday', 'to you']                                                                                
['happyn', 'n', 'birthdayn', 'to youn']   

Syntax

str.splitlines([keepends])

Parameters

Name Description
keepends Optional, if it is set True, line breaks are included in the items of the returned list

Return Value

It returns a list of lines in the string.

© 2024 Learnwithgpt.org, all rights reserved. Privacy Policy | Contact Us