Algorithm
-
Initialize an empty string variable:
- Create a variable to store the multiline string.
-
Add lines to the string:
- Use the
+=
operator to concatenate each line to the string variable.
- Use the
-
Ensure proper line breaks:
- If needed, use escape characters like
\n
to insert line breaks between each line.
- If needed, use escape characters like
-
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
The only way to
learn to program is
by writing code.
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
The only way to
learn to program is
by writing code.
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
The only way to
learn to program is
by writing code.
learn to program is
by writing code.
Demonstration
Python Programing Example to Create a Long Multiline String-DevsEnv