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.
How to remove newline character from text file?
python - Remove all line breaks from a long string of text - Stack Overflow
python - Python3: Remove \n from middle of the string while keeping the one in the end - Stack Overflow
How to remove newline in string-methods exercise?
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.
This is a JSON response. Instead of trying to mess with the string, you should use the json module to parse the string:
import json
result = b'{\n "destination_addresses" : ...'
obj = json.loads(obj.decode('utf-8'))
I am doing a exercise where i need to count the frequency of letters occur in a text. I downloaded a book from Gutenberg, to use as text file in project.
I have used strip() method to remove the newline character, but it's not working.
What i have tried so far:
fin = open('alice_wonderl.txt').read()
text = fin.strip('\n')When i printed the text, new line character is still there.
How do you enter line breaks with raw_input? But, once you have a string with some characters in it you want to get rid of, just replace them.
>>> mystr = raw_input('please enter string: ')
please enter string: hello world, how do i enter line breaks?
>>> # pressing enter didn't work...
...
>>> mystr
'hello world, how do i enter line breaks?'
>>> mystr.replace(' ', '')
'helloworld,howdoienterlinebreaks?'
>>>
In the example above, I replaced all spaces. The string '\n' represents newlines. And \r represents carriage returns (if you're on windows, you might be getting these and a second replace will handle them for you!).
basically:
# you probably want to use a space ' ' to replace `\n`
mystring = mystring.replace('\n', ' ').replace('\r', '')
Note also, that it is a bad idea to call your variable string, as this shadows the module string. Another name I'd avoid but would love to use sometimes: file. For the same reason.
You can try using string replace:
string = string.replace('\r', '').replace('\n', '')
I'm trying to remove the new line character from my strings but it's not working.
I've tried:
found_sentences = found_sentences.strip("\n")and
found_sentences = found_sentences.replace("\n"," ")
But the ultimate result still contains "\n" . What am I doing wrong?