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.
python - Remove all line breaks from a long string of text - Stack Overflow
How to remove newline character from text file?
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'))
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 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.
Try the method rstrip() (see doc Python 2 and Python 3)
>>> 'test string\n'.rstrip()
'test string'
Python's rstrip() method strips all kinds of trailing whitespace by default, not just one newline as Perl does with chomp.
>>> 'test string \n \r\n\n\r \n\n'.rstrip()
'test string'
To strip only newlines:
>>> 'test string \n \r\n\n\r \n\n'.rstrip('\n')
'test string \n \r\n\n\r '
In addition to rstrip(), there are also the methods strip() and lstrip(). Here is an example with the three of them:
>>> s = " \n\r\n \n abc def \n\r\n \n "
>>> s.strip()
'abc def'
>>> s.lstrip()
'abc def \n\r\n \n '
>>> s.rstrip()
' \n\r\n \n abc def'
And I would say the "pythonic" way to get lines without trailing newline characters is splitlines().
>>> text = "line 1\nline 2\r\nline 3\nline 4"
>>> text.splitlines()
['line 1', 'line 2', 'line 3', 'line 4']