86

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.

YakovL
  • 7,557
  • 12
  • 62
  • 102
user3290349
  • 1,227
  • 1
  • 9
  • 17

4 Answers4

131

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")
Bryan Oakley
  • 370,779
  • 53
  • 539
  • 685
42

Python 3: 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"
         f"This is line 3")  # no variable in this line, so a leading f"" is optional but can be used to properly align all lines

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 achieve 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:

helvete
  • 2,455
  • 13
  • 33
  • 37
winklerrr
  • 13,026
  • 8
  • 71
  • 88
22

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")
Diogo Martins
  • 917
  • 7
  • 15
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])
Dušan Maďar
  • 9,269
  • 5
  • 49
  • 64
  • 1
    This 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. – user3290349 Dec 15 '15 at 17:48
  • 3
    @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. – R Nar Dec 15 '15 at 17:49
  • 2
    @user3290349 in python there is no need for getters and setters. I suggest taking a look at python's `@property` decorator. https://docs.python.org/2/library/functions.html#property – Diogo Martins Dec 16 '15 at 15:51