Print \n in pthon

Posted on 2009-02-12
Last Modified: 2012-05-06
How would i do
print("name: daniel \n");

in Python????
Question by:dfernan
    LVL 12

    Expert Comment

    by:Sinoj Sebastian
    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

    Author Comment

    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?    
    LVL 28

    Expert Comment

    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.
    LVL 17

    Accepted Solution

    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.
    LVL 28

    Expert Comment

    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

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Less strange, but still introduction This introduction was added (1st August, 2011) to reflect some reactions.  Firstly, the term basics in the title of the article...  As any other word, it is a symbol with meaning attached to the word by some a…
    "The time has come," the Walrus said, "To talk of many things: Of sets--and lists--and dictionaries-- Of variable kinks-- And why you see it changing not-- And why so strange are strings." This part describes how variables and references (see …
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    Learn the basics of if, else, and elif statements in Python 2.7. Use "if" statements to test a specified condition.: The structure of an if statement is as follows: (CODE) Use "else" statements to allow the execution of an alternative, if the …

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now