How to Convert Multiline Strings to Single-Line Strings in Python
This guide explains how to convert multiline strings in Python into single-line strings. We'll cover removing extra whitespace, handling indentation, and choosing the best method for different scenarios.
Converting to a Single Line with splitlines()
and join()
The most common and versatile approach is to split the multiline string into lines, strip whitespace from each line, and then join the lines back together:
my_str = """\
First line
Second line
Third line
"""
result = " ".join(line.strip() for line in my_str.splitlines())
print(repr(result)) # Output: 'First line Second line Third line'
my_str.splitlines()
: Splits the multiline string into a list of individual lines. Importantly,splitlines()
handles different line endings (\n
,\r
,\r\n
) correctly across different operating systems.line.strip()
: Removes leading and trailing whitespace from each line. This is crucial for removing extra spaces and newline characters." ".join(...)
: Joins the processed lines back into a single string, using a single space as the separator. You can use any separator you want here (e.g.,""
for no separator,","
for a comma, etc.).- Generator Expression: The
(line.strip() for line in ...)
is a generator expression. It's memory-efficient, especially for very large strings, because it doesn't create an intermediate list.
Removing Leading/Trailing Whitespace and Empty Lines
The previous example already handles extra spaces within lines. If you also want to remove completely empty lines (lines containing only whitespace), include an additional check:
import os # Needed for os.linesep
multiline_string = """\
First line
Second line
Third line
"""
# Using a generator expression and os.linesep for cross-platform compatibility
without_empty_lines = os.linesep.join([
line for line in multiline_string.splitlines()
if line.strip() != ''
])
print(without_empty_lines)
Output:
First line
Second line
Third line
if line.strip() != ''
: This condition checks if the line, after stripping whitespace, is not empty. Only non-empty lines are included in the result.- Using
os.linesep
ensures the correct line separator is used on all platforms (Windows, macOS, Linux). This is more robust than hardcoding\n
.
Handling Indentation in Multiline Strings
Multiline strings defined within Python code often have leading whitespace due to indentation.
Using textwrap.dedent()
The textwrap.dedent()
function removes any common leading whitespace from each line in a string:
from textwrap import dedent
multiline_str = """\
first
second
third"""
print(dedent(multiline_str))
Output:
first
second
third
textwrap.dedent()
removes the common leading whitespace, preserving relative indentation within the string. It doesn't remove all leading whitespace, only the common indent.
Using inspect.cleandoc()
The inspect.cleandoc()
function goes a step further than textwrap.dedent()
: it also removes any empty lines at the beginning and end of the string:
from inspect import cleandoc
multiline_str = """
first
second
third
"""
# Removes leading/trailing blank lines AND common indentation
print(cleandoc(multiline_str))
#Output:
first
second
third
inspect.cleandoc()
is useful when you want to clean up multiline strings that might have extra blank lines due to how they're written in your code.