We help IT Professionals succeed at work.

Print \n in pthon

dfernan asked
Medium Priority
Last Modified: 2012-05-06
How would i do
print("name: daniel \n");

in Python????
Watch Question

Sinoj SebastianCTO & OpenERP Project manager

print "name: daniel \n"

by Default, print will add a newline in python. to avoid that, add a comma at the end on the statment

print "name: daniel "
print "name: daniel \n",
should give the same output


Can I concatenate instructions in python to avoid space and not have to always use tabs?????

I.e., instead of writing,

for x in range(10):
    If x<2:
        Print "x less than 2"
        hi = 5
        Bye = 3
        Print "x bigger than 2"

Write something like

For x in range(10){if(x<2){print "x less than 2";hi=5;bye=3;}else{print"x bigger than 2"}}

Is that possible in python? To write all the code in one line?    

For the \n. If you want to see whether a string contains \n or not, you can print the representation of the string. Try...

s = "name: daniel \n"
print repr(s)

If you want to use the same approach that you are used to from other languages, you can use sys.stdout.write() instead of print...

import sys
sys.stdout.write('name: daniel\n')

For your second question (Python is case sensitive -- Print is not print, if is not If), the simple answer is "not generally". Sometimes. But things in Python can also be done differently sometimes and the brevity may be the result of doing the ways another way.
Please avoid asking a 2nd question. That should be done in a new EE question.

It appears that you are providing examples written in some other language. What language is it?

In Python you may concatenate simple statements in a "suite" -
if a:print b;print c
but not compound statements.

You can accomplish part of your desire thusly:
for x in range(10):print "x less than 2" if x < 2 else "x bigger than 2"
However there is no simple way to accomplish conditional assignment.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts

Anyway, the conditional assingnment to some variables would not make sense in the loop. Also, other languages do not have the conditional assignment other than combination of if plus assignment or calling some special function (which can also be created in Python for the purpose).

Anyway, the question about "if possible to do one-liners" is quite usual for Python beginners. The "blocks by indentation" is not a problem after getting used to that. The opposite is often true. People who were not used to finally accept it as a good habit.

It should be emphasized that the good-style recommendation says "indent always by 4 spaces". It is possible to use also tabs; however, it is always better to get some decent editor that converts tab to 4 spaces (plus some smart indentation).
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.


Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.