How to create multi line string objects in python ?

In this article we will discuss different ways to create long multi line string in python.

Using triple quotes to create string of multiple lines

We can wrap the multi line string in triple quotes i.e. either <> and assign it to a string variable. It will be stored in same multi line format.
Checkout this example,

# Create string object from multiple lines
strObj = '''Hello this is a
               very long string
               indeed very long.'''

print(strObj)

Output:

Hello this is a
                   very long string
                   indeed very long.

Create single line string object from multiple lines

Using Brackets:

If we want to create a string object from long multiple lines but all should be stored in a single line then we should use brackets i.e.

# Create single line string object from multiple lines
strObj = ("Hello this is a "
          "very long string "
          "indeed very long.")

print(strObj)

Output:

Hello this is a very long string indeed very long.

Here all the given multiple lines are merged to a single line and assigned back to string variable.

Using Escape symbol:

We can create a single line string object from long multiple lines using escape character too i.e.

# Create single line string object from multiple lines
strObj = "Hello this is a " \
         "very long string " \
         "indeed very long."

print(strObj)

Output:

Hello this is a very long string indeed very long.

Here all the given multiple lines are merged to a single line and assigned back to string variable.

Using join() :

We can create a single line string object by joining multiple lines too i.e.

# Create single line string object from multiple lines
strObj = ''.join((
    "Hello this is a "
    "very long string "
    "indeed very long."
))
print(strObj)

Output:

Hello this is a very long string indeed very long.

Here all the given multiple lines are merged to a single line and assigned back to string variable.

Complete example is as follows:

def main():

    # Create string object from multiple lines
    strObj = '''Hello this is a
                   very long string
                   indeed very long.'''

    print(strObj)


    # Create single line string object from multiple lines
    strObj = ("Hello this is a "
              "very long string "
              "indeed very long.")

    print(strObj)

    # Create single line string object from multiple lines
    strObj = "Hello this is a " \
             "very long string " \
             "indeed very long."

    print(strObj)


    # Create single line string object from multiple lines
    strObj = ''.join((
        "Hello this is a "
        "very long string "
        "indeed very long."
    ))
    print(strObj)


if __name__ == '__main__':
    main()

Output:

Hello this is a
                   very long string
                   indeed very long.
Hello this is a very long string indeed very long.
Hello this is a very long string indeed very long.
Hello this is a very long string indeed very long.

 

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Scroll to Top