Open In App

Multiline String in Python

A sequence of characters is called a string. In Python, a string is a derived immutable data type—once defined, it cannot be altered. To change the strings, we can utilize Python functions like split, join, and replace.

Python has multiple methods for defining strings. Single quotations (”), double quotes (” “), and triple quotes (”’ ”’) are all acceptable.



Python Multiline String

There are several approaches to implementing the multiline string in Python. To define multi-line strings, we can use backlash, brackets, and triple quotes. To better understand the Python multiline string, below are the following approaches:

Python Multiline String Using Triple-Quotes

Using the triple quotes style is one of the easiest and most common ways to split a large string into a multiline Python string. Triple quotes (”’ or “””) can be used to create a multiline string. It allows you to format text over many lines and include line breaks. Put two triple quotes around the multiline Python string, one at the start and one at the end, to define it.






multiline_string = '''This is a
multiline
string.'''
print(multiline_string)

Output
This is a
multiline
string.






Create a Python Multiline String Using parentheses and single/double quotes

A different method to define a multiline string in Python is to include its components in brackets. Both single quotes (”) and double quotations (“”) are acceptable, as was previously shown. To learn how to use them, look at the examples provided below.




colors = ("multi-line string"
          "red \n"
          "blue \n"
          "green \n"
          "yellow \n"
          )
 
print(colors)

Output
multi-line stringred 
blue 
green 
yellow 







Python Multiline String Using Backslash

In Python, we can divide a string into many lines by using backslashes. The backslash character in Python serves as a line continuation character. It is used to combine text that consists of individual lines.




x = "multiline String" \
    "I love Python" \
    "Python Langauge"
print(x)

Output
multiline StringI love PythonPython Langauge






Create a Python Multiline String Using Brackets

When there is no newline in the string, there is another method for declaring a multiline string in Python that involves using brackets. Let’s examine it in the following example:




x = ("multiLine string \n"
     "Welcome to GFG \n"
     "I Love Coding  \n")
print(x)

Output
multiLine string 
Welcome to GFG 
I Love Coding  







Python Multiline String Creation Using join()

In this option, we break up multiple strings for printing multiline strings using Python’s string join() function. Because it would be difficult to skip a space when using brackets or backslashes in a very lengthy string, the two alternatives above included a condition to check for spaces when we use them. However, this issue may be fixed by utilizing the string join() method. Below is an example to help you understand it.




x = ' '.join(("multiline String ",
              "Python Language",
              "Welcome to GFG"))
print(x)

Output
multiline String  Python Language Welcome to GFG






Python Multiline String Using f-strings

Python introduces a new format for strings called F-strings, which can be used with Python 3.6 and later. This format is useful due to its shorter syntax. To use f-strings, insert a f or F before the string’s starting quotes. Variable names are specified in curly braces ({}) and their equivalent value is replaced at runtime.




name = "Welcome to GFG"
points = 10000
 
gfg = f"Hello! {name}, You got {points} points."
 
print(gfg)

Output
Hello! Welcome to GFG, You got 10000 points.






F-strings can also be used to format numbers, dates, and times.




price = 99.99
 
changed_price = f"${price:.2f}"
 
print(changed_price)

Output
$99.99






Multiline String in Python Using string.format()

In this option, we can use variables to format a string by using string.format(). This can be helpful for writing scripts, code, or other formatted text. Below is an example to help you understand it.




car = "Ferrari"
price = 250000
 
x = "Hello, The price of {} is {}".format(car, price)
 
print(x)

Output
Hello, The price of Ferrari is 250000






Multiline String Creation Using % in Python

In this option, Python’s % operator is used to format strings. It returns a formatted string after receiving as inputs a tuple of values and a format string. The values in the tuple are represented as substituted in the format string.




name = "Rahul"
points = 100
 
x = "Hello, %s! You have %d coins." % (name, points)
 
print(x)

Output
Hello, Rahul! You have 100 coins.






The % operator can also be used to format numbers, dates, and times.




marks = 49.99
 
total_marks = "%0.2f" % marks
 
print(total_marks)

Output
49.99







Article Tags :