Answers for "python split by newline"

2

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
Posted by: Guest on January-20-2021
0

how to split a string with newline in python

var = myString.splitlines()
Posted by: Guest on November-19-2020

Python Answers by Framework

Browse Popular Code Answers by Language