Concatenate strings in python in multiline
Solution 1
There are several ways. A simple solution is to add parenthesis:
strz = ("This is a line" +
str1 +
"This is line 2" +
str2 +
"This is line 3")
If you want each "line" on a separate line you can add newline characters:
strz = ("This is a line\n" +
str1 + "\n" +
"This is line 2\n" +
str2 + "\n" +
"This is line 3\n")
Solution 2
Solutions for Python 3 using Formatted Strings
As of Python 3.6 you can use so called "formatted strings" (or "f strings") to easily insert variables into your strings. Just add an f
in front of the string and write the variable inside curly braces ({}
) like so:
>>> name = "John Doe"
>>> f"Hello {name}"
'Hello John Doe'
To split a long string to multiple lines surround the parts with parentheses (()
) or use a multi-line string (a string surrounded by three quotes """
or '''
instead of one).
1. Solution: Parentheses
With parentheses around your strings you can even concatenate them without the need of a +
sign in between:
a_str = (f"This is a line \n{str1}\n"
f"This is line 2 \n{str2}\n"
"This is line 3") # no variable in this line, so no leading f
Good to know: If there is no variable in a line, there is no need for a leading f
for that line.
Good to know: You could archive the same result with backslashes (\
) at the end of each line instead of surrounding parentheses but accordingly to PEP8 you should prefer parentheses for line continuation:
Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
2. Solution: Multi-Line String
In multi-line strings you don't need to explicitly insert \n
, Python takes care of that for you:
a_str = f"""This is a line
{str1}
This is line 2
{str2}
This is line 3"""
Good to know: Just make sure you align your code correctly otherwise you will have leading white space in front each line.
By the way: you shouldn't call your variable str
because that's the name of the datatype itself.
Sources for formatted strings:
Solution 3
Python isn't php and you have no need to put $
before a variable name.
a_str = """This is a line
{str1}
This is line 2
{str2}
This is line 3""".format(str1="blabla", str2="blablabla2")
Solution 4
I would add everything I need to concatenate to a list and then join it on a line break.
my_str = '\n'.join(['string1', variable1, 'string2', variable2])
user3290349
Updated on April 05, 2022Comments
-
user3290349 about 2 years
I have some strings to be concatenated and the resultant string will be quite long. I also have some variables to be concatenated.
How can I combine both strings and variables so the result would be a multiline string?
The following code throws error.
str = "This is a line" + str1 + "This is line 2" + str2 + "This is line 3" ;
I have tried this too
str = "This is a line" \ str1 \ "This is line 2" \ str2 \ "This is line 3" ;
Please suggest a way to do this.
-
ipinak over 8 yearsWhich python are you using 2.x or 3?
-
user3290349 over 8 years@ipinak python 2.7.6
-
Russell Smith over 8 yearsdo you want each line to be a separate line (ie: should there be a newline after
"this is a line"
, and afterstr1
, etc?)
-
-
user3290349 over 8 yearsThis wouldnt solve my purpose.Because I am writing a script to which will take a variable name and generate a getter and setter method for it.SO I would want to write in the sae format.
-
R Nar over 8 years@user3290349 this solves the question that you asked, and has room for expanding. If you want it to solve a different purpose, ensure that purpose is stated in your question.
-
Patrick Carroll over 8 yearsThis will include the leading whitespace on each line, which is different than what the current code does.
-
Diogo Martins over 8 yearsI did not took that in consideration since the initial code provided didn't worked.
-
Diogo Martins over 8 yearsBtw... removing the leading whitespaces is as starting every line at col 1
-
Diogo Martins over 8 years@user3290349 in python there is no need for getters and setters. I suggest taking a look at python's
@property
decorator. docs.python.org/2/library/functions.html#property -
Scott Decker almost 6 yearsDon't forget the opening and closing parenthesis! That stumped me for a bit.