Python concat string multiple lines

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

  • What's new in Python 3.6
  • PEP498

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    Given pair of strings, which is multiline, perform concatenation horizontally.

    Examples:

    Input
    test_str1 = ”’ 
    geeks for 
    geeks”’, 
    test_str2 = ”’ 
    is 
    best”’ 
    Output
    geeks foris 
    geeksbest 
    Explanation : 1st line joined with 1st line of 2nd string, “geeks for” -> “is”.

    Input
    test_str1 = ”’ 
    geeks for ”’ 
    test_str2 = ”’ 
    geeks ”’ 
    Output
    geeks for geeks 
    Explanation : 1st line joined with 1st line of 2nd string, “geeks for ” -> “geeks”. 
     

    Method #1 : Using zip[] + split[] + join[] + list comprehension

    In this, we perform the task of splitting by “\n” using split[], and they are paired together using zip[]. The next step is joining both the zipped strings for horizontal orientation using “\n” and join[].

    Python3

    test_str1 =

    test_str2 =

    print["The original string 1 is : " + str[test_str1]]

    print["The original string 2 is : " + str[test_str2]]

    splt_lines = zip[test_str1.split['\n'], test_str2.split['\n']]

    res = '\n'.join[[x + y for x, y in splt_lines]]

    print["After String Horizontal Concatenation : " + str[res]]

    Output

    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest

    Time Complexity: O[n]

    Auxiliary Space: O[n]

    Method #2 : Using map[] + operator.add + join[]

    In this, we use map[] to with help of add[] to perform concatenation, split[] is used to perform initial split by “\n”.

    Python3

    from operator import add

    test_str1 =

    test_str2 =

    print["The original string 1 is : " + str[test_str1]]

    print["The original string 2 is : " + str[test_str2]]

    res = '\n'.join[map[add, test_str1.split['\n'], test_str2.split['\n']]]

    print["After String Horizontal Concatenation : " + str[res]]

    Output

    The original string 1 is : 
    geeks 4
    geeks
    The original string 2 is : 
    is
    best
    After String Horizontal Concatenation : 
    geeks 4is
    geeksbest

    Time Complexity: O[n] -> [ join function]

    Auxiliary Space: O[n]


    How do I concatenate a multiline string in Python?

    Use a backslash [ \ ] as a line continuation character Only string literals [string surrounded by ' or " ] are concatenated if written consecutively. Note that in the case of variables, an error is raised. Use the + operator to concatenate variables, or variables and string literals.

    How do you join strings between lines in Python?

    Python 3: Formatted Strings. 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].

    How do I concatenate two strings from different lines?

    Concatenation is the process of appending one string to the end of another string. You concatenate strings by using the + operator. For string literals and string constants, concatenation occurs at compile time; no run-time concatenation occurs.

    How do you concatenate 3 strings in Python?

    Concatenation means joining strings together end-to-end to create a new string. To concatenate strings, we use the + operator. Keep in mind that when we work with numbers, + will be an operator for addition, but when used with strings it is a joining operator.

    Chủ Đề