results_str.replace('\n', "") 

You're so close. The \ is an escape character in string literals, so you must either double it up, or use a raw string:

results_str.replace('\\n', "") 
results_str.replace(r'\n', "") 

Also remember that Python strings are immutable: replace() returns a new string, because the existing string cannot be modified. If you want to keep the changed string, you have to assign it to some name, such as the original name:

results_str = results_str.replace(r'\n', "")

Although you may want to process your data in a fashion that doesn't care about the newlines. That looks like JSON, so rather than trying to parse it yourself you should probably be using the Python json module.

Answer from kindall on Stack Overflow
๐ŸŒ
Sentry
sentry.io โ€บ sentry answers โ€บ python โ€บ how do i remove a trailing newline from a string in python
How do I remove a trailing newline from a string in Python | Sentry
August 15, 2023 - We can use lstrip and rstrip together to remove leading and trailing characters while preserving characters in the middle of the string, as below: my_string = "\r\n\n Hello world!
๐ŸŒ
GeeksforGeeks
geeksforgeeks.org โ€บ python โ€บ python-removing-newline-character-from-string
Removing newline character from string in Python - GeeksforGeeks
July 12, 2025 - Regular expressions allow us to find and replace patterns in text. The re.sub() function can match newline characters and remove them. ... The re.sub() function looks for all matches of the pattern \n in text.
Discussions

How to remove newline character from text file?
As the doc for str.strip() explains, the function: Return a copy of the string with the leading and trailing characters removed. So your code probably did remove the trailing newline at the end of the file because the .read() returns a single string. You can probably get what you want by using the str.replace() method to replace every newline character with a space. I really recommend that you try testing your code on a small test file of three or four lines and putting print() statements in to check what is happening. Like this: fin = open('alice_wonderl.txt').read() print(f'fin:\n{fin}') text = fin.replace('\n', ' ') print(f'text:\n{text}') More on reddit.com
๐ŸŒ r/learnpython
6
2
August 13, 2020
python - Remove all line breaks from a long string of text - Stack Overflow
Basically, I'm asking the user to input a string of text into the console, but the string is very long and includes many line breaks. How would I take the user's string and delete all line breaks to More on stackoverflow.com
๐ŸŒ stackoverflow.com
February 19, 2019
python - Python3: Remove \n from middle of the string while keeping the one in the end - Stack Overflow
I am new to Python and stuck with this problem. I have a multiline string: My name is ABCD \n I am 20 years old \n I like to travel his name is XYZ \n he is 20 years old \n he likes to eat your na... More on stackoverflow.com
๐ŸŒ stackoverflow.com
How to remove newline in string-methods exercise?
Hi, how can i remove this new line space using strip()? or is there any other way? More on discuss.codecademy.com
๐ŸŒ discuss.codecademy.com
0
0
May 18, 2020
๐ŸŒ
Syntx Scenarios
syntaxscenarios.com โ€บ home โ€บ python โ€บ how to remove newline from python string(7 easy methods)
How to Remove Newline From Python String(7 Easy Methods)
March 7, 2026 - To use it, call replace() on the string. The first parameter is the character to remove, and the second parameter is an empty string (""), which removes the newline completely. Incorrect line breaks may sometimes cause syntax errors, such as ...
๐ŸŒ
Python Guides
pythonguides.com โ€บ remove-newlines-from-a-string-in-python
Remove Newline Characters from a String in Python
September 15, 2025 - I have a string named paragraph, and I need to remove a newline from the string. So first, I created an empty string and then used the split() method with \n as a parameter so that it would make a list separated by the \n character, like this: โ€œIt was a bright day in Dallas. The sun was shiningโ€. I can also use the splitlines() method of Python.
๐ŸŒ
Java2Blog
java2blog.com โ€บ home โ€บ python โ€บ python string โ€บ remove newline from string in python
Remove NewLine from String in Python [4 ways] - Java2Blog
November 25, 2023 - The strip() function truncates the trailing newline characters (\n) and white spaces, which means it eliminates newline characters & whitespaces from both ends of the specified string (start and end). ... To remove newlines specifically, strip(โ€˜\n\rโ€™) can be used.
๐ŸŒ
Sabe
sabe.io โ€บ blog โ€บ python-remove-newline-from-strings
How to Remove Newlines from Strings in Python - Sabe.io
October 2, 2022 - PYTHONstring = "\nHello, World!\n" stripped = string.replace("\n", "") print(stripped) BASHHello, World! Keep in mind that this method will remove all newline characters from the string, not just the beginning and end. In this post, we learned how to remove newline characters from strings in Python.
Find elsewhere
๐ŸŒ
Statistics Globe
statisticsglobe.com โ€บ home โ€บ python programming language for statistics & data science โ€บ strip newline in python | 4 example codes (remove trailing & leading blank line)
Strip Newline in Python | 4 Examples (Remove Blank Line from String)
March 30, 2022 - In order to delete both, the trailing and leading newlines, we can apply the strip Python function: # Remove trailing and leading newlines from string my_string_updated_all = my_string.strip()
๐ŸŒ
Easy Tweaks
easytweaks.com โ€บ python-remove-newline-string
How to remove newlines from a string in Python 3?
Master meetings, chats, channels and online collaboration ยท Go beyond the basics in Word, Excel, PowerPoint and Outlook
๐ŸŒ
Delft Stack
delftstack.com โ€บ home โ€บ howto โ€บ python โ€บ python remove newline from string
How to Remove Newline From String in Python | Delft Stack
February 2, 2024 - ... Here, original_string is the string from which you want to remove leading and trailing whitespaces. By calling strip('\n'), we instruct Python to remove any leading and trailing newline characters.
๐ŸŒ
Saturn Cloud
saturncloud.io โ€บ blog โ€บ how-to-remove-newlines-from-messy-strings-in-pandas-dataframe-cells
How to Remove Newlines from Messy Strings in Pandas DataFrame Cells | Saturn Cloud Blog
May 1, 2026 - For example, if you are working with a CSV file that contains newlines in the middle of a cell, it can cause errors when trying to read the file into a DataFrame. Additionally, newlines can make it difficult to extract or manipulate data in the affected cells. One way to remove newlines from a string is to use Pythonโ€™s built-in replace() method.
๐ŸŒ
Codedamn
codedamn.com โ€บ news โ€บ python
How to remove newline from a string in Python
June 3, 2023 - We call the replace() method on this string, passing in the newline character \n as the first argument, and an empty string "" as the second argument. This tells Python to replace all instances of the newline character with an empty string, ...
๐ŸŒ
Stack Overflow
stackoverflow.com โ€บ questions โ€บ 74587200
python - Python3: Remove \n from middle of the string while keeping the one in the end - Stack Overflow
@lediwa - You could use a raw string so that \n isn't converted into newlines. text = r""" (yourstring) """ and then do replace(r'\n', ' ') (so that would be the two characters \ and n). ... Save this answer.
๐ŸŒ
DEV Community
dev.to โ€บ itsmycode โ€บ python-remove-newline-from-string-5f7g
Python Remove Newline From String - DEV Community
December 13, 2021 - Similarly, if we need to replace inside newline characters in a list of strings, we can iterate it through for loop and use a replace() function to remove the newline characters. # Python code to remove newline character from string using replace() method text = "A regular \n expression is a sequence \n of characters\n that specifies a search\n pattern." print(text.replace('\n', '')) my_list = ["Python\n", "is\n", "Fun\n"] new_list = [] print("Original List: ", my_list) for i in my_list: new_list.append(i.replace("\n", "")) print("After removal of new line ", new_list)
๐ŸŒ
Stack Abuse
stackabuse.com โ€บ bytes โ€บ remove-trailing-newlines-in-python
Remove Trailing Newlines in Python
September 11, 2023 - The Python splitlines() method can be a handy tool when dealing with strings that contain newline characters. This method splits a string into a list where each element is a line of the original string. ... As you can see, splitlines() effectively removes the trailing newline from the string.
๐ŸŒ
Better Stack
betterstack.com โ€บ community โ€บ questions โ€บ how-to-remove-trailing-new-line-in-python
How do I remove a trailing newline in Python? | Better Stack Community
February 3, 2023 - You can use the rstrip() method to remove a trailing newline from a string. Here is an example: ... This will print "Hello, World!" (without the newline). Alternatively, you can use the strip() method to remove both leading and trailing whitespace, ...