{"id":24654,"date":"2021-10-25T09:12:17","date_gmt":"2021-10-25T03:42:17","guid":{"rendered":"https:\/\/python-programs.com\/?p=24654"},"modified":"2021-11-05T19:15:01","modified_gmt":"2021-11-05T13:45:01","slug":"python-string-splitlines-method-with-examples","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-string-splitlines-method-with-examples\/","title":{"rendered":"Python String splitlines() Method with Examples"},"content":{"rendered":"
In the previous article, we have discussed Python String maketrans() Method with Examples<\/a> The splitlines() method divides(splits) a string into a list of lines. Line breaks are used for splitting.<\/p>\n Syntax:<\/strong><\/p>\n Parameters<\/strong><\/p>\n keeplinebreaks:<\/strong> Optional. Specifies whether or not line breaks should be included (True) (False). False is the default value.<\/p>\n Return Value:\u00a0<\/strong><\/p>\n splitlines() returns a list of the string’s lines.<\/p>\n If no line break characters are present, it returns a list with a single item (a single line).<\/p>\n splitlines() divides lines at the following points:<\/strong><\/p>\n Examples:<\/strong><\/p>\n Example1:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example2:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Example3:<\/strong><\/p>\n Input:<\/strong><\/p>\n Output:<\/strong><\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Go through our tutorial and learn about various Python String Method Examples<\/a> and learn how to apply the knowledge while dealing with strings.<\/p>\n 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. …<\/p>\n
\nsplitlines() Method in Python:<\/strong><\/p>\nstring.splitlines(keeplinebreaks)<\/pre>\n
\n
Given string = 'welcome to\\n Python\\n programs\\n'<\/pre>\n
The given string after spliting : ['welcome to', ' Python', ' programs']<\/pre>\n
Given string = 'hello all good morning'<\/pre>\n
The given string after spliting : ['hello all good morning']<\/pre>\n
Given string = 'welcome to\\n Python\\n programs\\n'<\/pre>\n
The given string after spliting by passing the true parameter:\r\n['welcome to\\n', ' Python\\n', ' programs\\n']<\/pre>\n
String splitlines() Method Examples in Python<\/h2>\n
\n
Method #1: Using Built-in Functions (Static Input)<\/h3>\n
\n
# Give the string as static input and store it in a variable.\r\ngvn_str = 'welcome to\\n Python\\n programs\\n'\r\n# Apply splitlines() function to the given string that divides(splits) a\r\n# given string into a list of lines.\r\n# Store it in another variable.\r\nsplt_str = gvn_str.splitlines()\r\n# Print the result after splitting the given string.\r\nprint(\"The given string after spliting :\", splt_str)\r\n# Apply splitlines() function to the given string by passing the parameter\r\n# as True and printing the result.\r\nprint(\"The given string after spliting :\", gvn_str.splitlines(True))\r\n# Give the second string as static input and store it in a variable.\r\ngvn_scndstr = 'hello all good morning'\r\n# Apply splitlines() function to the given second string and print the result.\r\nprint(\"The given string after spliting :\", gvn_scndstr.splitlines())\r\n<\/pre>\n
The given string after spliting : ['welcome to', ' Python', ' programs']\r\nThe given string after spliting : ['welcome to\\n', ' Python\\n', ' programs\\n']\r\nThe given string after spliting : ['hello all good morning']<\/pre>\n
\n