Breaking a Long string into multiple lines














































Breaking a Long string into multiple lines



 

            Breaking a long string into multiple lines, can be done in various methods,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. 

 
  USING TRIPLE QUOTES : 
 
     

"""This is a long
multi line string """
     
     
      Anything inside the triple quotes will become part of the string.
     
     
line_str = "I'm learning Python.\nI refer to Cppsecrets.com tutorials.\nIt is the best site for Python programmers."
print("Long string with newlines: \n" + line_str)


multiline_str = """I'm learning Python.
I refer to Cppsecrets.com tutorials.
It is the best site for Python programmers."""
print("Multiline string: \n" + multiline_str)


     
      The output is as follows.
     
 	     
I'm learning Python.
I refer to Cppsecrests.com tutorials.
It is the best  site for Python programmers.

I'4m learning Python.
I refer to Cppsecrests.com tutorials.
It is the best  site for Python programmers. 
USNG BRACKETS  :
Another technique is to enclose the slices of a string over multiple lines using brackets. See the                       below example to know how to use this method.

multiline_str = ("I'm learning Python. "
"I refer to Cppsecrets.com tutorials. "
"It is the best site for Python programmers.")
print(multiline_str)

multiline_str = ("I'm learning Python.\n"
"I refer to Cppsecrets.com tutorials.\n"
"It is the best site for Python programmers.")
print(multiline_str)

The output is as follows.
		I'm learning Python. I refer to Cppsecrets.com tutorials. It is the best site for Python programmers.

I'm learning Python.
I refer to Cppsecrets.com tutorials.
It is the best site for Python programmers.
USING BACKSLASH TO JOIN STRING ON MULTIPLE LINES :


multiline_str = "I'm learning Python. "
"I refer to Cppsecrets.com tutorials. "
"It is the best site for Python programmers."
print(multiline_str)

The output is as follows.
		I'm learning Python. I refer to Cppsecrets.com tutorials. It is the best site for Python programmers.
JOIN METHOD TO CREATE STRING WITH NEW LINES   :
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.

multiline_str = ' '.join(("I'm learning Python.",
"I refer to Cppsecrets.com tutorials.",
"It is the best site for Python programmers."))
print(multiline_str)


multiline_str = ''.join(("I'm learning Python.\n",
"I refer to Cppsecrets.com tutorials.\n",
"It is the best site for Python programmers."))
print(multiline_str)
The output is as follows.
		I'm learning Python. I refer to Cppsecrets.com tutorials. It is the best site for Python programmers.
I'm learning Python.
I refer to Cppsecrets.com tutorials.
It is the best site for Python programmers.



Comments