Output, concatenate, split, delete, and replace strings containing newlines in Python

Money and Business

The following describes the operation of strings containing newlines in Python.

  • Create string containing newlines, print output (display)
    • newline character (either or both of CR and LF depending on system)\n(LF), \r\n(CR+LF)
    • triple quote''',"""
    • If you want to indent
  • Concatenate a list of strings with newlines
  • Split string into newlines and list:splitlines()
  • Remove and replace line feed codes
  • Print output without trailing newline

Create string containing newlines, print output

newline character (either or both of CR and LF depending on system) \n(LF), \r\n(CR+LF)

Inserting a line feed code within a string will result in a new line.

s = 'Line1\nLine2\nLine3'
print(s)
# Line1
# Line2
# Line3

s = 'Line1\r\nLine2\r\nLine3'
print(s)
# Line1
# Line2
# Line3

Line feed codes can be used in the following ways. Some editors allow you to select a line break code.

Macを含むUnix系\n(LF)
Windows系\r\n(CR+LF)

triple quote ''',"""

If triple quotes are used to enclose the string, it will be a string as is, including newlines.

s = '''Line1
Line2
Line3'''
print(s)
# Line1
# Line2
# Line3

If you want to indent

Triple quotes are also spaces in a string, so if you try to write neatly in code and indent as shown below, unnecessary spaces will be inserted.

s = '''
    Line1
    Line2
    Line3
    '''
print(s)
# 
#     Line1
#     Line2
#     Line3
#     

By using a backslash to ignore newlines in the code and make it a continuation line, it can be written as follows

Enclose each line with '' or “” and add a newline character ←n at the end of the sentence.

s = 'Line1\n'\
    'Line2\n'\
    'Line3'
print(s)
# Line1
# Line2
# Line3

Here, the syntax is that successive string literals are concatenated. See the following article for details.

If you want to add indentation in a string, just add a space to the string in each line.

s = 'Line1\n'\
    '    Line2\n'\
    '        Line3'
print(s)
# Line1
#     Line2
#         Line3

In addition, since line breaks can be freely made in brackets, the following can be written using parentheses instead of backslashes.

s = ('Line1\n'
     'Line2\n'
     'Line3')
print(s)
# Line1
# Line2
# Line3

s = ('Line1\n'
     '    Line2\n'
     '        Line3')
print(s)
# Line1
#     Line2
#         Line3

If you just want to align the beginning of a line, just add a backslash to the first line of triple quotes.

s = '''\
Line1
Line2
Line3'''
print(s)
# Line1
# Line2
# Line3

s = '''\
Line1
    Line2
        Line3'''
print(s)
# Line1
#     Line2
#         Line3

Concatenate a list of strings with newlines

The string method join() can be used to concatenate a list of strings into a single string.

When join() is called from a newline character, each string element is concatenated with a newline.

l = ['Line1', 'Line2', 'Line3']

s_n = '\n'.join(l)
print(s_n)
# Line1
# Line2
# Line3

print(repr(s_n))
# 'Line1\nLine2\nLine3'

s_rn = '\r\n'.join(l)
print(s_rn)
# Line1
# Line2
# Line3

print(repr(s_rn))
# 'Line1\r\nLine2\r\nLine3'

As in the example above, the built-in function repr() can be used to check strings that contain newline codes as they are.

Split string into newlines and list: splitlines()

The string method splitlines() can be used to split a string into a list of newlines.

splitlines() will split any of the following line break codes. Vertical tabs and page breaks are also split.

  • \n
  • \r\n
  • \v
  • \f
s = 'Line1\nLine2\r\nLine3'
print(s.splitlines())
# ['Line1', 'Line2', 'Line3']

Remove and replace line feed codes

By combining splitlines() and join(), it is possible to remove (remove) newline codes from a string containing newlines or replace them with other strings.

s = 'Line1\nLine2\r\nLine3'

print(''.join(s.splitlines()))
# Line1Line2Line3

print(' '.join(s.splitlines()))
# Line1 Line2 Line3

print(','.join(s.splitlines()))
# Line1,Line2,Line3

Batch change of line feed codes is also possible. Even if line break codes are mixed or unknown, they can be split using splitlines() and then concatenated with the desired line break code.

s_n = '\n'.join(s.splitlines())
print(s_n)
# Line1
# Line2
# Line3

print(repr(s_n))
# 'Line1\nLine2\nLine3'

As mentioned above, splitlines() will split either newline code, so there is no need to be particularly concerned about newline codes in the case of the method combining splitlines() and join().

If the newline code is clear, it can also be replaced by the replace() method, which replaces the string.

s = 'Line1\nLine2\nLine3'

print(s.replace('\n', ''))
# Line1Line2Line3

print(s.replace('\n', ','))
# Line1,Line2,Line3

Note, however, that it will not work if it contains different line feed codes than expected.

s = 'Line1\nLine2\r\nLine3'

s_error = s.replace('\n', ',')
print(s_error)
# ,Line3Line2

print(repr(s_error))
# 'Line1,Line2\r,Line3'

s_error = s.replace('\r\n', ',')
print(s_error)
# Line1
# Line2,Line3

print(repr(s_error))
# 'Line1\nLine2,Line3'

It is possible to replace multiple newline codes by repeating replace(), but it will not work if the order is wrong because “\r\n” contains “\n”. The method combining splitlines() and join() described above is safer because there is no need to worry about line feed codes.

s = 'Line1\nLine2\r\nLine3'

print(s.replace('\r\n', ',').replace('\n', ','))
# Line1,Line2,Line3

s_error = s.replace('\n', ',').replace('\r\n', ',')
print(s_error)
# ,Line3Line2

print(repr(s_error))
# 'Line1,Line2\r,Line3'

print(','.join(s.splitlines()))
# Line1,Line2,Line3

Use the rstrip() method to remove line feed codes at the end of a sentence. rstrip() is a method to remove white space characters (including line feeds) at the right end of a string.

s = 'aaa\n'
print(s + 'bbb')
# aaa
# bbb

print(s.rstrip() + 'bbb')
# aaabbb

Print output without trailing newline

The print() function adds a trailing newline by default. Therefore, if print() is executed in succession, each output result will be displayed on a new line.

print('a')
print('b')
print('c')
# a
# b
# c

This is because the default value of the argument end of print(), which specifies the string to be added at the end, is the newline symbol.

If you do not want a newline at the end, just set the argument end to an empty string, and the output will be output without a newline at the end.

print('a', end='')
print('b', end='')
print('c', end='')
# abc

The argument end can be any string.

print('a', end='-')
print('b', end='-')
print('c')
# a-b-c

However, if you want to concatenate strings for output, it is easier to concatenate the original strings than to specify them in the end argument of print(). See the following article.

Copied title and URL