7

I have the following string:

string = """
Hello World
123
HelloWorld
"""

I want to clear all the line-breaks from the string in python.

I tried

string.strip()

But it's not working as desired.

What I should do?

I'm using python 3.3

Thanks.

2
  • string.replace('\n', '') not working? Commented Nov 3, 2013 at 18:59
  • The fact that the string is triple-quoted has nothing to do with the problem, and it is also not version-specific. There are two separate problems that could be intended here: one about simply creating a string literal that doesn't have new lines in it (but is wrapped over multiple lines of source code), and one about actively removing newlines from a string (not necessarily a literal) after the fact. Commented Apr 22, 2024 at 21:27

5 Answers 5

24

I would like to point out that, depending on how that triple-quoted string is being used, you could avoid the issue entirely.

In Python triple-quoted strings, you can put a backslash ("\") at the end of a line to ignore the line break. In other words, you can use it to put a line break at that spot in your code without making a line break in your string.

For example:

"""line 1 \
line 2 \
line 3"""

will actually become

line 1 line 2 line 3

if printed or written to some file or other output.

Using it this way can eliminate any need for a function to replace the line breaks, making your code clearer and cleaner.

EDIT:

If you're using backslash line continuations like this, you can also use simple single-quoted strings the same way;

"line 1 \
line 2 \
line 3"

is also equivalent to

line 1 line 2 line 3
Sign up to request clarification or add additional context in comments.

2 Comments

Why would one need triple quotes AND backslash? Why not just backslash?
@AllDaniSpringer You could also do a single quoted string and it would work the same way; I just kept the triple quotes because that's what the question was asking about. I'll make an edit to mention that, though.
11

str.strip removes whitespace from the start and the end of the string.

>>> string
'\nHello World\n123\nHelloWorld\n'
>>> string.strip()
'Hello World\n123\nHelloWorld'

If you want to remove the new line characters inside of the string, you can replace them by something else using str.replace:

>>> string.replace('\n', ' ')
' Hello World 123 HelloWorld '

Comments

5

Use translate instead of replace for this kind of operation.

>> string.translate(None, "\n")
Hello World123HelloWorld

You will realize how fast translate is when you work with big files.

Comments

0

Simple str.replace:

string.replace('\n', ' ')

Comments

0

The main question is already answered, but I would like to add that if you plan on using the individual lines you might be better off doing this -

string = """
Hello World
123
HelloWorld
"""
for line in string.strip().split('\n'):
    print(line)
    # do something with the line

Output from above code -

Hello World
123
HelloWorld

4 Comments

That’s no different to print(string.strip()).
But what if he wants to use individual lines?
That would be a different problem and nothing in the question suggests OP wants to do that.
I just wanted to show another way of doing it while describing it's specific use. Partial answer.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.