How do i put two strings in one line in python?

target.write(line1 \n, line2 \n, line3 \n)

'\n' only make sense inside a string literal. Without the quotes, you don't have string literals.

target.write('line1 \n, line2 \n, line3 \n')

Ok, now everything is a string literal. But you want line1, line2, line3 to not be string literals. You need those as python expressions to refer the variables in question. Basically, you have to put quotes around strings that are actually text like "\n" but not around variables. If you did that, you might have gotten something like:

target.write(line1 '\n' line2 '\n' line3 '\n')

What is 2 2? It's nothing. You have to specify to python how to combine the two pieces. So you can have 2 + 2 or 2 * 2 but 2 2 doesn't make any sense. In this case, we use add to combine two strings

target.write(line + '\n' + line2 + '\n' + line3 + '\n')

Moving on,

target.write(%r \n, %r \n, %r \n) % (line1, line2, line3)

Again \n only makes sense inside a string literal. The % operator when used to produce strings takes a string as its left side. So you need all of that formatting detail inside a string.

target.write('%r \n', '%r \n', '%r \n') % (line1, line2, line3)

But that produce 3 string literals, you only want one. If you did this, write complained because it excepts one string, not 3. So you might have tried something like:

target.write('%r \n%r \n%r \n') % (line1, line2, line3)

But you want to write the line1, line2, line3 to the file. In this case, you are trying to the formatting after the write has already finished. When python executes this it will run the target.write first leaving:

None % (line1, line2, line3)

Which will do nothing useful. To fix that we need to to put the % () inside the .write()

target.write('%r\n%r\n%r\n' % (line1, line2, line3))

This tutorial explains how to create a Python multiline string. It can be handy when you have a very long string. You shouldn’t keep such text in a single line. It kills the readability of your code.

In Python, you have different ways to specify a multiline string. You can have a string split across multiple lines by enclosing it in triple quotes. Alternatively, brackets can also be used to spread a string into different lines.

Moreover, backslash works as a line continuation character in Python. You can use it to join text on separate lines and create a multiline string. Finally, there is string join() function in Python which is used to produce a string containing newlines.

Python String

Let’s now discuss each of these options in details. We have also provided examples with the description of every method.

Use triple quotes to create a multiline string

How do i put two strings in one line in python?

It is the simplest method to let a long string split into different lines. You will need to enclose it with a pair of Triple quotes, one at the start and second in the end.

"""Learn Python
Programming"""

Anything inside the enclosing Triple quotes will become part of one multiline string. Let’s have an example to illustrate this behavior.

# String containing newline characters
line_str = "I'm learning Python.\nI refer to TechBeamers.com tutorials.\nIt is the most popular site for Python programmers."

Now, we’ll try to slice it into multiple lines using triple quotes.

# String containing newline characters
line_str = "I'm learning Python.\nI refer to TechBeamers.com tutorials.\nIt is the most popular site for Python programmers."
print("Long string with newlines: \n" + line_str)

# Creating a multiline string
multiline_str = """I'm learning Python.
I refer to TechBeamers.com tutorials.
It is the most popular site for Python programmers."""
print("Multiline string: \n" + multiline_str)

After running the above, the output is:

Long string with newlines: 
I'm learning Python.
I refer to TechBeamers.com tutorials.
It is the most popular site for Python programmers.
Multiline string: 
I'm learning Python.
I refer to TechBeamers.com tutorials.
It is the most popular site for Python programmers.

This method retains the newline ‘\n’ in the generated string. If you want to remove the ‘\n’, then use the strip()/replace() function.

Use brackets to define a multiline string

How do i put two strings in one line in python?

Another technique is to enclose the slices of a string over multiple lines using brackets. See the below example to know how to use it:

# Python multiline string example using brackets
multiline_str = ("I'm learning Python. "
"I refer to TechBeamers.com tutorials. "
"It is the most popular site for Python programmers.")
print(multiline_str)

It provides the following result:

I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.

You can see there is no newline character in the output. However, if you want it, then add it while creating the string.

# Python multiline string with newlines example using brackets
multiline_str = ("I'm learning Python.\n"
"I refer to TechBeamers.com tutorials.\n"
"It is the most popular site for Python programmers.")
print(multiline_str)

Here is the output after execution:

I'm learning Python.
I refer to TechBeamers.com tutorials.
It is the most popular site for Python programmers.

Please note that PEP 8 guide recommends using brackets to create Python multiline string.

Backslash to join string on multiple lines

How do i put two strings in one line in python?

It is a less preferred way to use backslash for line continuation. However, it certainly works and can join strings on various lines.

# Python multiline string example using backslash
multiline_str = "I'm learning Python. " \
"I refer to TechBeamers.com tutorials. " \
"It is the most popular site for Python programmers."
print(multiline_str)

The above code gives the following result:

I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.

You can observe that the output isn’t showing any newlines. However, you may like to add some by yourself.

# Python multiline string example using backslash and newlines
multiline_str = "I'm learning Python.\n" \
"I refer to TechBeamers.com tutorials.\n" \
"It is the most popular site for Python programmers."
print(multiline_str)

The output:

I'm learning Python.
I refer to TechBeamers.com tutorials.
It is the most popular site for Python programmers.

Join() method to create a string with newlines

How do i put two strings in one line in python?

The final approach is applying the string join() function to convert a string into multiline. It handles the space characters itself while contaminating the strings.

# Python multiline string example using string join()
multiline_str = ' '.join(("I'm learning Python.",
                          "I refer to TechBeamers.com tutorials.",
                          "It is the most popular site for Python programmers."))
print(multiline_str)

It outputs the following result:

I'm learning Python. I refer to TechBeamers.com tutorials. It is the most popular site for Python programmers.
# Python multiline string with newlines example using string join()
multiline_str = ''.join(("I'm learning Python.\n",
                          "I refer to TechBeamers.com tutorials.\n",
                          "It is the most popular site for Python programmers."))
print(multiline_str)

The result is:

I'm learning Python.
I refer to TechBeamers.com tutorials.
It is the most popular site for Python programmers.

We hope that after wrapping up this tutorial, you should feel comfortable in using Python multiline string. However, you may practice more with examples to gain confidence.

Also, to learn Python from scratch to depth, do read our step by step Python tutorial.

How do you write two strings in one line in Python?

Python Program to Create a Long Multiline String.
my_string = '''The only way to learn to program is by writing code.''' print(my_string) ... .
my_string = ("The only way to \n" "learn to program is \n" "by writing code.") print(my_string).

How do you merge strings in Python?

Use the + operator.
str1="Hello".
str2="World".
print ("String 1:",str1).
print ("String 2:",str2).
str=str1+str2..
print("Concatenated two different strings:",str).

How do you concatenate multiple lines in Python?

Use a backslash ( \ ) as a line continuation character If a backslash is placed at the end of a line, it is considered that the line is continued on the next line. Only string literals (string surrounded by ' or " ) are concatenated if written consecutively. Note that in the case of variables, an error is raised.

What is the best way to concatenate strings in Python?

One of the most popular methods to concatenate two strings in Python (or more) is using the + operator. The + operator, when used with two strings, concatenates the strings together to form one.