In the previous article, we have discussed Python String maketrans() Method with Examples
splitlines() Method in Python:
The splitlines() method divides(splits) a string into a list of lines. Line breaks are used for splitting.
Syntax:
string.splitlines(keeplinebreaks)
Parameters
keeplinebreaks: Optional. Specifies whether or not line breaks should be included (True) (False). False is the default value.
Return Value:Â
splitlines() returns a list of the string’s lines.
If no line break characters are present, it returns a list with a single item (a single line).
splitlines() divides lines at the following points:
- \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
Examples:
Example1:
Input:
Given string = 'welcome to\n Python\n programs\n'
Output:
The given string after spliting : ['welcome to', ' Python', ' programs']
Example2:
Input:
Given string = 'hello all good morning'
Output:
The given string after spliting : ['hello all good morning']
Example3:
Input:
Given string = 'welcome to\n Python\n programs\n'
Output:
The given string after spliting by passing the true parameter: ['welcome to\n', ' Python\n', ' programs\n']
String splitlines() Method Examples in Python
Method #1: Using Built-in Functions (Static Input)
Approach:
- Give the string as static input and store it in a variable.
- Apply splitlines() function to the given string that divides(splits) a given string into a list of lines.
- Store it in another variable.
- Print the result after splitting the given string.
- Apply splitlines() function to the given string by passing the parameter as True and printing the result.
- Give the second string as static input and store it in a variable.
- Apply splitlines() function to the given second string and print the result.
- The Exit of Program.
Below is the implementation:
# Give the string as static input and store it in a variable. gvn_str = 'welcome to\n Python\n programs\n' # Apply splitlines() function to the given string that divides(splits) a # given string into a list of lines. # Store it in another variable. splt_str = gvn_str.splitlines() # Print the result after splitting the given string. print("The given string after spliting :", splt_str) # Apply splitlines() function to the given string by passing the parameter # as True and printing the result. print("The given string after spliting :", gvn_str.splitlines(True)) # Give the second string as static input and store it in a variable. gvn_scndstr = 'hello all good morning' # Apply splitlines() function to the given second string and print the result. print("The given string after spliting :", gvn_scndstr.splitlines())
Output:
The given string after spliting : ['welcome to', ' Python', ' programs'] The given string after spliting : ['welcome to\n', ' Python\n', ' programs\n'] The given string after spliting : ['hello all good morning']
Go through our tutorial and learn about various Python String Method Examples and learn how to apply the knowledge while dealing with strings.