Algorithm


  1. Initialize an empty string variable:

    • Create a variable to store the multiline string.
  2. Add lines to the string:

    • Use the += operator to concatenate each line to the string variable.
  3. Ensure proper line breaks:

    • If needed, use escape characters like \n to insert line breaks between each line.
  4. Print or use the multiline string as needed:

    • Once the string is constructed, you can print it or use it in your program as required.

 

Code Examples

#1 Code Example- Python Programing Using triple quotes

Code - Python Programming

my_string = '''The only way to
learn to program is
by writing code.'''

print(my_string)
Copy The Code & Try With Live Editor

Output

x
+
cmd
The only way to
learn to program is
by writing code.

#2 Code Example- Python Programing Using parentheses and a single/double quotes

Code - Python Programming

my_string = ("The only way to \n"
        	"learn to program is \n"
        	"by writing code.")

print(my_string)
Copy The Code & Try With Live Editor

Output

x
+
cmd
The only way to
learn to program is
by writing code.

#3 Code Example- Python Programing Using \

Code - Python Programming

my_string = "The only way to \n" \
        	"learn to program is \n" \
        	"by writing code."

print(my_string)
Copy The Code & Try With Live Editor

Output

x
+
cmd
The only way to
learn to program is
by writing code.
Advertisements

Demonstration


Python Programing Example to Create a Long Multiline String-DevsEnv