• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1490
  • Last Modified:

Print \n in pthon

How would i do
print("name: daniel \n");

in Python????
1 Solution
Sinoj SebastianCommented:
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
dfernanAuthor Commented:
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.
ramromconsultant Commented:
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.
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).

Featured Post

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now