Multiline strings python – 4 Methods for Creating Python Multiline Strings

Multiline Strings in Python:

Multiline strings python: To generate multiline strings in Python, utilise brackets, backslashes, and triple quotes, but the user must specify the use of spaces between the strings.

The Python string.join() function is thought to be a particularly efficient way to generate multiline strings, and it also handles spaces between the strings implicitly.

We can create multiline strings in the following ways:

  • Using Triple quotes
  • Using backslash(\)
  • Using string.join() method
  • Using round brackets ()

1)Using Triple quotes

Python multiline string: In Python, triple quotes can be used to display many strings at the same time(together), i.e. multiline strings.

If our input contains string statements with too many characters, triple quotes can help us show it in a proper manner.
Everything between the triple quotes is regarded as the string itself.

Syntax:

" " " strings" " "

Example

# Give the string as static input and store it in a variable.
gvn_str = """hello this is btechgeeks
good morning this is btechgeeks. welcome to btechgeeks"""
# Print the given string
print("The given string is:\n", gvn_str)

Output:

The given string is:
 hello this is btechgeeks
good morning this is btechgeeks. welcome to btechgeeks

2)Using backslash(\)

Python multiline strings: In Python, the escape sequence backslash (“) can also be is used to construct multiline strings.

Syntax:

variable = "string_1"\"string_2"\"string_N"

When using a backslash( \) to create multiline strings, the user must explicitly mention the spaces between the strings.

Example

# Give the string as static input and store it in a variable.
gvn_str = "hello this is btechgeeks"\
"good morning this is btechgeeks"\
"welcome to btechgeeks"
# Print the given string
print("The given string is:\n", gvn_str)

Output:

The given string is:
hello this is btechgeeksgood morning this is btechgeekswelcome to btechgeeks

3)Using string.join() method

Python f string multiline: The Python string.join() function has shown to be an efficient method for creating Python multiline strings.

The string.join() method handles and manipulates all of the gaps/spaces between the strings, so the user does not need to be concerned.

Syntax:

string.join(("string_1","string_2","string_N"))

Example

# Pass multiple strings to the join() function to join all the strings(creating
# multiline strings)
gvn_str = ''.join(("hello this is btechgeeks",
                   "good morning this is btechgeeks", "welcome to btechgeeks"))
# Print the given string
print("The given string is:\n", gvn_str)

Output:

The given string is:
 hello this is btechgeeksgood morning this is btechgeekswelcome to btechgeeks

4)Using round brackets ()

Multiline string python: Python brackets can be used to generate multiline strings and split or separate the strings together.

The main disadvantage of this method is that the user must manually provide the spaces between the multiline strings.

Syntax:

variable = ("string_1""string_2""string_N")

Example

# Pass multiple strings to the join() function to join all the strings(creating
# multiline strings)
gvn_str = ("hello this is btechgeeks"
           "good morning this is btechgeeks"
           "welcome to btechgeeks")
# Print the given string
print("The given string is:\n", gvn_str)

Output:

The given string is:
 hello this is btechgeeksgood morning this is btechgeekswelcome to btechgeeks

In Brief:

  • Python multiline strings are strings that have been divided onto numerous lines to improve the readability of the code for users.
  • To generate multiline strings in Python, utilise brackets, backslashes, and triple quotes, but the user must specify the use of spaces between the strings.
  • The Python string.join() function is thought to be a particularly efficient way to generate multiline strings, and it also handles spaces between the strings implicitly.
  • Multiline strings are not subject┬áto Python’s indentation rules.(not applicable for them)
  • If a multiline string is formed with triple quotes, all escape sequences such as newline(\n) and tab-space(\t) are treated to be part of the string.