how to split a line into new line p yhon code example

Example 1: python split by newline

# Using str.splitlines() method, you can split a string by new lines

# Example 1
string = "Line 1\nLine 2\nLine 3\nLine4"
string = string.splitlines()
print(string)
# OUTPUT
['Line 1', 'Line 2', 'Line 3', 'Line4']

# Example 2
string = """Hello world.
My name is bob

I like dogs"""
string = string.splitlines()
print(string)
# OUTPUT
['Hello world.', 'My name is bob', '', 'I like dogs']

# 2.X Documentation: https://docs.python.org/2/library/stdtypes.html#str.splitlines
# 3.X Documentation: https://docs.python.org/3/library/stdtypes.html#str.splitlines

Example 2: how to split a string with newline in python

var = myString.splitlines()

Tags:

Java Example