Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Extract lines from user's input paragraphs

Tags:

python

input

I get a user input using:

paragraphInput = input ("paste your paragraph ")
print(paragraphInput)

I get:

Line 1 of the paragraph
Line 2 of the paragraph
Line 3 of the paragraph
Line 4 of the paragraph

I would like to have displayed something like this:

This is Line 1 of the paragraph !
This is Line 2 of the paragraph !
This is Line 3 of the paragraph !
This is Line 4 of the paragraph !

So I wanted to use a for loop but I don't know how to retrieve the line "n" of the paragraph and then to add in front of it This is and after it ! . Is there a way of doing this ? Because the number of lines of the paragraphs will change depending on the user ...

Thanks for taking the time to read and for your help !

like image 330
Graham Slick Avatar asked Sep 03 '25 09:09

Graham Slick


2 Answers

Python has a function to split lines of a string.

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

For example, 'ab c\n\nde fg\rkl\r\n'.splitlines() returns ['ab c', '', 'de fg', 'kl'],

Then you can just iterate through this list that now has proper line breaks.

for line in paragraphInput.splitlines():
    print "This is " + line + " !"
like image 69
AdriVelaz Avatar answered Sep 04 '25 22:09

AdriVelaz


You can do

for line in paragraphInput.split('\n'):
    print "This is " + line + " !"
like image 38
James Avatar answered Sep 04 '25 22:09

James