Sling Academy
Home/Python/Python: 4 Ways to Declare a Multiline String (Block String)

Python: 4 Ways to Declare a Multiline String (Block String)

Last updated: May 25, 2023

This concise, straight-to-the-point article shows you a couple of different ways to declare a multiline string (block string) in Python.

Using Triple Quotes (Recommended)

You can use triple quotes (""" or ''') to declare multiline strings. This method allows you to include line breaks within the string without the need for explicit escape characters.

Example (triple double-quotes):

multiline_string = """This is a
multiline
string."""

Another example (triple single-quotes):

multiline_string = '''This is a
multiline
string.'''

This approach is simple, intuitive, and widely used in modern Python projects.

If you want to include variables or expressions in a multiline string, you can use f-strings with triple quotes. With this, you can perform string interpolation within the multiline string.

Example:

name = "Wolf"
age = 99

multiline_string = f"""My name is {name}.
I am {age} years old."""

Using the Backslash Character

The slash / is an escape character that tells Python to ignore the following character and treat it as part of the string. You can use it to split a long string into multiple lines without creating a newline character.

Example:

multiline_string = "This is a \
multiline \
string."

This technique works; it is not very readable or clear that you are creating a single string from multiple lines. Another important thing you should be aware of is that It does not preserve the indentation or alignment in the original code.

Using Parentheses for Implicit Line Continuation

Another way to declare a multiline string is by enclosing the string in parentheses. By doing so, you can write the string across multiple lines without the need for explicit line continuation characters.

Example:

multiline_string = (
    "Welcome "
    "to "
    "Sling Academy."
)

This approach works, but the line breaks or indentations in the original code won’t be maintained. When you print the string above, you will get this result:

Welcome to Sling Academy.

Using the join() Method

You can use the join() method to concatenate multiple strings in a list. By breaking the strings into separate lines and joining them with line breaks, you can create a multiline string.

Example:

multiline_string = '\n'.join([
    "This is a",
    "multiline",
    "string."
])

I think this technique is more verbose than necessary. I put it here for your reference, but using triple quotes is far better.

Next Article: Python: Capitalize the First Letter of each Word in a String

Previous Article: Python: 4 Ways to Convert a String into a List

Series: Working with Strings in Python

Python

You May Also Like

  • Introduction to yfinance: Fetching Historical Stock Data in Python
  • Monitoring Volatility and Daily Averages Using cryptocompare
  • Advanced DOM Interactions: XPath and CSS Selectors in Playwright (Python)
  • Automating Strategy Updates and Version Control in freqtrade
  • Setting Up a freqtrade Dashboard for Real-Time Monitoring
  • Deploying freqtrade on a Cloud Server or Docker Environment
  • Optimizing Strategy Parameters with freqtrade’s Hyperopt
  • Risk Management: Setting Stop Loss, Trailing Stops, and ROI in freqtrade
  • Integrating freqtrade with TA-Lib and pandas-ta Indicators
  • Handling Multiple Pairs and Portfolios with freqtrade
  • Using freqtrade’s Backtesting and Hyperopt Modules
  • Developing Custom Trading Strategies for freqtrade
  • Debugging Common freqtrade Errors: Exchange Connectivity and More
  • Configuring freqtrade Bot Settings and Strategy Parameters
  • Installing freqtrade for Automated Crypto Trading in Python
  • Scaling cryptofeed for High-Frequency Trading Environments
  • Building a Real-Time Market Dashboard Using cryptofeed in Python
  • Customizing cryptofeed Callbacks for Advanced Market Insights
  • Integrating cryptofeed into Automated Trading Bots