Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I split a multi-line string into multiple lines?

I have a multi-line string that I want to do an operation on each line, like so:

inputString = """Line 1 Line 2 Line 3""" 

I want to iterate on each line:

for line in inputString:     doStuff() 
like image 515
bradtgmurray Avatar asked Oct 05 '08 18:10

bradtgmurray


People also ask

How do you split text into lines in Python?

To split the line in Python, use the String split() method. The split() is an inbuilt method that returns a list of lines after breaking the given string by the specified separator. In this tutorial, the line is equal to the string because there is no concept of a line in Python. So you can think of a line as a string.


1 Answers

inputString.splitlines() 

Will give you a list with each item, the splitlines() method is designed to split each line into a list element.

like image 160
UnkwnTech Avatar answered Oct 07 '22 13:10

UnkwnTech