4 ways to create Multiline String in Python

Python String

You can represent strings in Python by using multiple ways. Python multi-line string is the way of string statements with multiple lines. It is the best way to present multi-line strings in a well-formatted and arranged manner. In this post, we will explore 4 ways to create Multiline String in Python

Important Points to Remember:


  • In Python brackets, backslash, and triple quotes are use to create mutilines.Users will have to manage use of spaces between the strings manually.
  • In the Python string.join() method handles the spaces between the strings implicitly.It is best way to create mutiline string.
  • In Python indentation rules are not applicable to multiline strings.

1. Triple quotes to create Multiline String


Using the Triple quotes We enclosed string in pair of triple-double quotes ” ” ” or single quotes(‘ ‘ ‘) at beginning and end and assign it to a variable. Python treats all strings enclosed between triple quotes as a single string itself.

In Python triple quotes technique , newline(\n) , tab-space(\t) are considered as part of string.

Synatx

sentence = """ multi line strings """

Program Example

Welcome_note = """Welcome to devenum,How are you,
good to see you again.
Thanks for visiting us again.
We are creating a mutiline string in python.
Python is vast used programming lanaguage in Machine Learning.."""
print(Welcome_note )

Output:

Welcome to devenum,How are you,
good to see you again.
Thanks for visiting us again.
We are creating a mutiline string in python.
Python is vast used programming lanaguage in Machine Learning.

Splice string into mutiple line Using newline charcater

If we need to split the long string into a multiline string then to slice a string in multiline we can use the new line character(\n).

Welcome_note = """Welcome to devenum,How are you,\ngood to see you again.\nThanks for visiting us again.\nWe are creating a mutiline string in python.\nPython is vast used programming lanaguage in Machine Learning."""
print(Welcome_note )

Output

Welcome to devenum,How are you,
good to see you again.
Thanks for visiting us again.
We are creating a mutiline string in python.
Python is vast used programming lanaguage in Machine Learning.

2: Using backslash


The backslash is also known as an escape sequence(‘\’). It is used to create multiline strings in python. We can pass the string enclosed in double-quotes separated by escape sequence(‘\’)

Syntax

My_Intro = "string1"\"string2"…..\"stringN"

Program Example:

#Program to Create Python Multiline Strings

My_Intro = "Welcome to devenum," \
"How are you." \
"good to see you again." \
"Thanks for visiting us again" \
"We are creating a mutiline string in python"
print(My_Intro )

Output:

Welcome to devenum,How are you.good to see you again.Thanks for visiting us againWe are creating a mutiline string in python

Splice string into mutiple line Using newline charcater

To create a multiline string we can create a newline character(\n).

#Program to Create Python Multiline Strings

My_Intro = "Welcome to devenum,\n" \
"How are you.\n" \
"good to see you again.\n" \
"Thanks for visiting us again.\n" \
"We are creating a mutiline string in python \n"
print(My_Intro )

Output

Welcome to devenum,
How are you.
good to see you again.
Thanks for visiting us again.
We are creating a mutiline string in python 

3. string.join() to create a multiline string


Using the string.join() method we can create a multiline string in Python. We can pass multiple strings as arguments to a string.join() method. These strings should be separated by a comma and enclosed inside in double-quotes.

Syntax

string.join(("string1","string2",…,..,"stringN"))

Program Example

multiline_note = ' '.join(( "Welcome to devenum,How are you",
"good to see you again.",
"Thanks for visiting us again.",
"We are creating a mutiline string in python.",
"Python is vast used programming lanaguage in Machine Learning."))
print(multiline_note)

Output:

Welcome to devenum,How are you good to see you again.Thanks for visiting us again. We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning.

Splice string into mutiple line Using newline charcater

If we need to split a string, we can do it by using the special character (\n).

multiline_note = ' '.join(( "Welcome to devenum,How are you",
"good to see you again.\n",
"Thanks for visiting us again.\n",
"We are creating a mutiline string in python.\n",
"Python is vast used programming lanaguage in Machine Learning.\n"))
print(multiline_note)

Output

Welcome to devenum,How are you good to see you again.
 Thanks for visiting us again.
 We are creating a mutiline string in python.
 Python is vast used programming lanaguage in Machine Learning.

4: Using brackets() to create multiline strings


By using brackets() we can create multiline strings. We can pass the slice of string to create a multiline string.

Syntax

My_Intro = ("string1""string2" ".." "..""stringN")

Program Example:

My_Intro = ("Welcome to devenum,How are you"
"good to see you again."
"Thanks for visiting us again."
"We are creating a mutiline string in python. Python is vast used programming lanaguage in Machine Learning")
print((My_Intro )

Output:

I am here to teach you the topic about how to write multiple line strings in python language.
And i will be teaching you 4 different techniques to create multiline strings using python

Summary

In this post, We have understood 4 techniques to Create Python Multiline Strings in Python. Now using this technique we can easily generate a muti line string in Python.