A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.tutorialspoint.com/python/string_splitlines.htm below:

Python String splitlines() Method

Python String splitlines() Method

The Python String splitlines() method breaks a string at line boundaries. These line boundaries are a part of Universal newlines. Various line boundaries this method recognizes are given below:

Note These line breaks will be not displayed in the output unless specified.

Syntax

Following is the syntax for Python String splitlines() method −

str.splitlines()
Parameters Return Value

This method returns a list with all the lines in string, optionally including the line breaks (if num is supplied and is true).

Example

When the method is called on the string containing the line breaks, the output is returned as the split string.

The following example shows the usage of Python String splitlines() method.

str1 = "Names:\nAlex\nJohn\nRichard\nNick"
print("String before splitting: " + str1)
print("String after splitting:")
print(str1.splitlines())
str2 = "Names:\rAlex\rJohn\rRichard\rNick"
print("String before splitting: " + str2)
print("String after splitting:")
print(str2.splitlines())

When we run above program, it produces following result −

String before splitting: Names:
Alex
John
Richard
Nick
String after splitting:
['Names:', 'Alex', 'John', 'Richard', 'Nick']
String before splitting: Names:
Alex
John
Richard
Nick
String after splitting:
['Names:', 'Alex', 'John', 'Richard', 'Nick']
Example

If you pass True as a parameter to this method this includes the line breaks in the output.

In the following example, we will call the splitlines() method on an input string and pass the value "true" as an argument.

str1 = "Names:\nAlex\nJohn\nRichard\nNick"
print("String before splitting: " + str1)
print("String after splitting:")
print(str1.splitlines(True))
str2 = "Names:\rAlex\rJohn\rRichard\rNick"
print("String before splitting: " + str2)
print("String after splitting:")
print(str2.splitlines(True))

When we run above program, it produces following result

String before splitting: Names:
Alex
John
Richard
Nick
String after splitting:
['Names:\n', 'Alex\n', 'John\n', 'Richard\n', 'Nick']
String before splitting: Names:
Alex
John
Richard
Nick
String after splitting:
['Names:\r', 'Alex\r', 'John\r', 'Richard\r', 'Nick']

python_strings.htm


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4