python replace n with actual new line code example

Example 1: python replace newline

without_line_breaks = a_string.replace("\n", " ")

Example 2: python replace n with actual new line

#Just print the text and copy from console
print(f"A \n B \n C")
#output:
# A
# B
# C

Example 3: python replace newline

from tika import parser

filename = 'myfile.pdf'

# Parse the PDF
parsedPDF = parser.from_file(filename)

# Extract the text content from the parsed PDF
pdf = parsedPDF["content"]

# Convert double newlines into single newlines
pdf = pdf.replace('\n\n', '\n')

#####################################
# Do something with the PDF
#####################################
print (pdf)

Example 4: python replace \n with something else

>>> import re
>>> re.sub('\r?\n', ' $ ', 'a\r\nb\r\nc')
'a $ b $ c'
>>> re.sub('\r?\n', ' $ ', 'a\nb\nc')
'a $ b $ c'