Home > other >  python: how to convert input string with multiple newlines as a single paragraph
python: how to convert input string with multiple newlines as a single paragraph

Time:07-28

I need to take user input as string, which may/may not have multiple newline characters. But I need to modify this input in a single newline input. How can I achieve this without seeing the actual user input, preferably without any for loop.

input

"Walk a little slower, Daddy!" said a little child so small.
"I'm following in your footsteps and i don't want to fall.

Sometimes your steps are very fast, sometimes they're hard to see;
So walk a little slower Daddy, for you are leading me.

Someday when I'm all grown up, you're what i want to be.
Then i will have a little child who'll want to follow me.

And i would want to lead just right, and know that i was true;
So, walk a little slower, Daddy, for i must follow you!!"

output

"Walk a little slower, Daddy!" said a little child so small."I'm following in your footsteps and i don't want to fall.Sometimes your steps are very fast, sometimes they're hard to see;So walk a little slower Daddy, for you are leading me.
Someday when I'm all grown up, you're what i want to be.Then i will have a little child who'll want to follow me.And i would want to lead just right, and know that i was true;So, walk a little slower, Daddy, for i must follow you!!"

CodePudding user response:

There are several ways to achieve this:

I will stick with your example:

input_string = """
"Walk a little slower, Daddy!" said a little child so small.
"I'm following in your footsteps and i don't want to fall.

Sometimes your steps are very fast, sometimes they're hard to see;
So walk a little slower Daddy, for you are leading me.

Someday when I'm all grown up, you're what i want to be.
Then i will have a little child who'll want to follow me.

And i would want to lead just right, and know that i was true;
So, walk a little slower, Daddy, for i must follow you!!"
"""

First you can use Python's replace function.

out_string = input_string.replace("\n", " ").strip()

Output: ' "Walk a little slower, Daddy!" said a little child so small. "I'm following in your footsteps and i don't want to fall. Sometimes your steps are very fast, sometimes they're hard to see; So walk a little slower Daddy, for you are leading me. Someday when I'm all grown up, you're what i want to be. Then i will have a little child who'll want to follow me. And i would want to lead just right, and know that i was true; So, walk a little slower, Daddy, for i must follow you!!" '

Note, that in this case, there might be spaces left in the beginning and the end of the text and there might also be several spaces in a row, depending on the position of the line brakes. The upside of this method is, that it is very fast.

A slower, but more sophisticated method is to use regex:

import re

RE_NEWLINE = re.compile(r"\n ")
out_string = RE_NEWLINE.sub(" ", input_string).strip()

Output:

"Walk a little slower, Daddy!" said a little child so small. "I'm following in your footsteps and i don't want to fall. Sometimes your steps are very fast, sometimes they're hard to see; So walk a little slower Daddy, for you are leading me. Someday when I'm all grown up, you're what i want to be. Then i will have a little child who'll want to follow me. And i would want to lead just right, and know that i was true; So, walk a little slower, Daddy, for i must follow you!!"

As you can see, this methods does not produce multiple spaces in a row, because the regex r"\n " handles multiple 'newlines'

Which method will be the best, also depends on the further processing.

  • Related