Python Forum
\n not working - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: \n not working (/thread-37478.html)



\n not working - WouldWerker - Jun-15-2022

Why does the following code work in my CLI, but not on my local Apache web server?

#!C:\Python310\python.exe
print ("Content-type: text/html\n\n")
str1="Hi there, Chris."
str2="It works!"
print(str1 + "\n" + str2)
the web server output is "Hi there, Chris. It works!" (w/o the quotation marks)


RE: \n not working - rob101 - Jun-15-2022

'\n' is not recognized as a new line (AKA a line break). For HTML, you need to use <br>

Or you could create it with CSS


RE: \n not working - ndc85430 - Jun-15-2022

Please explain exactly what isn't working and show all relevant code. There's nothing there that really has anything to do with a web server (other than a string that looks like an HTTP header, but it's just a string). Don't make us guess, please.


RE: \n not working - DeaD_EyE - Jun-16-2022

(Jun-15-2022, 07:48 PM)WouldWerker Wrote: Why does the following code work in my CLI, but not on my local Apache web server?

Your Command Line Interface sees the \n as newline character and go to the next line.
The Web Browser doesn't. The Web Browser want to get HTML Content. Try out what rob101 said.
A simple \n should work in <pre>\n</pre> tags, but they are used to displaying monospaced fonts (good for code), but all other tags like p or div don't interpret the newline character. Instead, <html-tags> are interpreted, and they can be nested. Replace \n with <br />.


In addition: If you want to program a web application, this is the wrong way.
You need a framework like Flask or Django and a Templating System like Jinja2 to create from Data + Template -> HTML-Page with data

They way you did it, is from the 90s. In early days, code was written like you did.


RE: \n not working - WouldWerker - Jun-16-2022

Thank you all for your help. I have just started learning Python, so I'm a complete newby. My goal is to use it for data analytics including website dev. So I'll include Flask in my learning time.

Again, thank you all.

PS. I apologize for not being more descriptive with my issue. I'll do better with the next question :).