python new line (byte to string)
<p>Code:</p>

<pre><code>word="hello\nhai"
fout=open("sample.txt","w");
fout.write(word);
</code></pre>

<p>Output:</p>

<pre><code>hello
hai
</code></pre>

<p>But this:</p>

<pre><code>word=b"hello\nhai"
str_word=str(word)                     # stripping ' '(quotes) from byte
str_word=str_word[2:len(str_word)-1]   # and storing just the org word
fout=open("sample.txt","w");
fout.write(str_word);
</code></pre>

<p>Outputs:</p>

<pre><code>hello\nhai
</code></pre>

<p>What is the problem in the code?</p>

<p>I am working on sending and receiving strings over a port in python. As only bytes can be sent and received I have the above problem. But why does it occur?</p>