Advertisement
# The signature of splitlines() method
splitlines(keepnewline)
Parameters | Description |
---|---|
keepnewline | This optional parameter is used to specify a bool value where - -- The value True specifies that the newline character \n should be included as a part of a split. -- The value False specifies that the newline character should not be included in the split. -- The default value of this parameter is False. |
# Python - Example of splitlines() method.
# Creating a string
s1 = 'Do not give up yet \n Get up \n Work hard!'
# Calling the splitlines() method on string
# which will perform the number of splits(starting at the right)
# on the basis of default separator i.e. a newline character \n
# and returns a list containing all the split values
a_list = s1.splitlines()
# Priting all the split values
print('A list of splits : ', a_list)
A list of splits : ['Do not give up yet ', ' Get up ', ' Work hard!']
Advertisement
# Python - Example of splitlines() method.
# Creating a string
s1 = 'No beauty \n Shines Brighter \n Than \n A bright heart!'
# Calling the splitlines() method on string
# which will perform the number of splits(starting at the left)
# on the basis of default separator i.e. a newline character \n
# and returns a list containing all the split values
# bool True specifies that newline character should be included as a part of split
a_list = s1.splitlines(True)
# Priting all the split values
print('A list of splits with newline included in each split: ', a_list)
# Calling the splitlines() method on string
# which will perform the number of splits(starting at the left)
# on the basis of default separator i.e. a newline character \n
# and returns a list containing all the split values
# bool True specifies that newline character should not be included as a part of split
a_list = s1.splitlines(False)
# Priting all the split values
print('A list of splits with newline not included in each split: ', a_list)
A list of splits with newline included in each split: ['No beauty \n', ' Shines Brighter \n', ' Than \n', ' A bright heart!']
A list of splits with newline not included in each split: ['No beauty ', ' Shines Brighter ', ' Than ', ' A bright heart!']
Advertisement
Advertisement
Please check our latest addition
C#, PYTHON and DJANGO
Advertisement