Last updated: Apr 9, 2024
Reading timeยท5 min
To convert a multiline string to a single line:
str.splitlines()
method to get a list of the lines in the string.str.strip()
method to remove leading and trailing whitespace from
each line.join()
method to join the list with a space separator.my_str = """\ First line Second line Third line """ result = " ".join(line.strip() for line in my_str.splitlines()) print(repr(result)) # ๐๏ธ "First line Second line Third line"
We used the str.splitlines()
method to get a list of the lines in the string.
my_str = """\ First line Second line Third line """ # ๐๏ธ ['First line', 'Second line', 'Third line'] print(my_str.splitlines())
The method splits the string on universal (Windows and Unix) newline characters.
We used a generator expression to iterate over the list of strings.
On each iteration, we use the str.strip()
method to remove any leading and
trailing whitespace.
my_str = """\ First line Second line Third line """ result = " ".join(line.strip() for line in my_str.splitlines()) print(repr(result)) # ๐๏ธ "First line Second line Third line"
The str.strip() method returns a copy of the string with the leading and trailing whitespace removed.
The last step is to join the list of strings into a string.
The str.join() method takes an iterable as an argument and returns a string which is the concatenation of the strings in the iterable.
In the example, we joined the strings in the list with a space separator.
However, you could also use a tab.
my_str = """\ First line Second line Third line """ result = "\t".join(line.strip() for line in my_str.splitlines()) print(repr(result)) # ๐๏ธ "'First line\tSecond line\tThird line'"
Use the textwrap.dedent()
method to strip the leading whitespace from a
multiline string in Python.
The textwrap.dedent
method will remove the common leading whitespace from
every line of the string.
from textwrap import dedent from inspect import cleandoc multiline_str = """\ first second third""" # ๐๏ธ remove indentation # first # second # third print(dedent(multiline_str)) # ๐๏ธ removes indentation and empty lines at the beginning and end # first # second # third print(cleandoc(multiline_str))
The first example uses the textwrap.dedent
method to remove the leading
whitespace from the multiline string.
The textwrap.dedent() method takes a multiline string and removes the common leading whitespace from every line of the string.
Note that we used a backslash at the end of the first line of the multiline string.
multiline_str = """\ first second third"""
Make sure to close the multiline string on the same line.
multiline_str = """\ first second third""" # ๐๏ธ close on same line print(multiline_str)
If you don't close the multiline string on the same line, an extra newline character gets added at the end of the string.
If your multiline string has empty lines at the beginning or end, use the inspect.cleandoc() method to remove them and remove the leading whitespace.
from inspect import cleandoc multiline_str = """ first second third """ # ๐๏ธ removes indentation and empty lines at beginning and end # first # second # third print(cleandoc(multiline_str))
We didn't use a backslash at the end of the first line of the string and didn't close the multiline string on the same line, so the string has an empty line at the beginning and at the end.
The inspect.cleandoc()
method takes care of removing the empty lines at the
beginning and end and the leading whitespace.
If you don't want to
remove the empty lines at the
beginning and end of the multiline string, use the textwrap.dedent()
method.
To properly indent multiline strings:
dedent()
and indent()
methods if you need to dedent or indent the
multiline string.from textwrap import dedent, indent from inspect import cleandoc multiline_str = """\ first second third""" # ๐๏ธ with indentation # first # second # third print(multiline_str) # ๐๏ธ without indentation # first # second # third print(dedent(multiline_str)) # ๐๏ธ indent the multiline string a specific number of spaces # first # second # third print(indent(multiline_str, ' ')) # ๐๏ธ using inspect.cleandoc # (removes empty lines at beginning and end, and leading whitespace) # first # second # third print(cleandoc(multiline_str))
The first thing to note when using multiline strings is to add a backslash at the end of the first line.
multiline_str = """\ first second third""" print(multiline_str)
Make sure to close the multiline string on the same line.
multiline_str = """\ first second third""" # ๐๏ธ close on same line print(multiline_str)
If you don't close the multiline string on the same line, an extra newline character gets added at the end of the string.
textwrap.indent()
and textwrap.dedent()
methods to indent or dedent the multiline string.from textwrap import dedent, indent multiline_str = """\ first second third""" # ๐๏ธ without indentation # first # second # third print(dedent(multiline_str)) # ๐๏ธ indent the multiline string a specific number of spaces # first # second # third print(indent(multiline_str, ' '))
The textwrap.dedent() method takes a multiline string and removes the common leading whitespace from every line of the string.
The method is used to display multiline strings that are indented in the source code without any indentation.
The textwrap.indent() method takes a multiline string and a prefix and adds the prefix to the beginning of each line of the string.
By default, the method adds the prefix to the beginning of each line that doesn't consist only of whitespace.
You can also use the inspect.cleandoc
method if you want to remove the empty
lines at the beginning and end of the multiline string and the leading
whitespace.
from inspect import cleandoc multiline_str = """ first second third """ # # first # second # third # print(multiline_str) # first # second # third print(cleandoc(multiline_str))
I intentionally didn't add a backslash at the end of the first line and didn't close the multiline string on the same line.
Notice that the inspect.cleandoc() method removed the empty lines at the beginning and end of the string and removed the leading whitespace.
If you don't want to remove the empty lines at the beginning and end of the
multiline string, use the textwrap.dedent()
method.
You can learn more about the related topics by checking out the following tutorials: