Results 1 to 7 of 7

Thread: [SOLVED] [Python] Single Line Output for the 'for' loop

  1. #1
    Join Date
    Aug 2006
    Beans
    458
    Distro
    Ubuntu 12.04 Precise Pangolin

    [SOLVED] [Python] Single Line Output for the 'for' loop

    This segment in Python
    Code:
    for i in range(0,10):
        print i
    produces the same result as this piece of code in C++
    Code:
    for(i=0;i<10;i++)
       cout<<i<<"\n";
    What should I do in Python to get the output similar to that in C++ but without the newline character? (0123456789)

    Thanks
    Last edited by navneeth; June 25th, 2008 at 12:44 PM.

  2. #2
    Join Date
    Aug 2006
    Location
    60°27'48"N 24°48'18"E
    Beans
    3,458

    Re: [Python] Single Line Output for the 'for' loop

    This is a slightly obscure bit of Python, but what you essentially do is

    Code:
    print x,
    The comma omits the newline.
    LambdaGrok. | #ubuntu-programming on FreeNode

  3. #3
    Join Date
    Oct 2006
    Location
    Austin, Texas
    Beans
    2,715

    Re: [Python] Single Line Output for the 'for' loop

    Code:
    from sys import stdout
    
    stdout.write("Hello ")
    stdout.write("world!")
    Or, just join all your strings together and print them that way...

    Code:
    print "".join(str(i) for i in xrange(10))

  4. #4
    Join Date
    Sep 2006
    Beans
    2,914

    Re: [Python] Single Line Output for the 'for' loop

    Code:
    >>> print ''.join(map(str,range(10)))
    0123456789

  5. #5
    Join Date
    Jan 2006
    Beans
    Hidden!
    Distro
    Ubuntu 10.10 Maverick Meerkat

    Re: [Python] Single Line Output for the 'for' loop

    wrong thread
    I am infallible, you should know that by now.
    "My favorite language is call STAR. It's extremely concise. It has exactly one verb '*', which does exactly what I want at the moment." --Larry Wall
    (02:15:31 PM) ***TimToady and snake oil go way back...
    42 lines of Perl - SHI - Home Site

  6. #6
    Join Date
    Aug 2006
    Beans
    458
    Distro
    Ubuntu 12.04 Precise Pangolin

    Re: [Python] Single Line Output for the 'for' loop

    Quote Originally Posted by CptPicard View Post
    This is a slightly obscure bit of Python, but what you essentially do is

    Code:
    print x,
    The comma omits the newline.
    Quote Originally Posted by Wybiral View Post
    Code:
    from sys import stdout
    
    stdout.write("Hello ")
    stdout.write("world!")
    Or, just join all your strings together and print them that way...

    Code:
    print "".join(str(i) for i in xrange(10))
    Quote Originally Posted by ghostdog74 View Post
    Code:
    >>> print ''.join(map(str,range(10)))
    0123456789
    Great! Thank you, all. I learnt a couple of new things, as well.

  7. #7
    Join Date
    Jun 2006
    Location
    CT, USA
    Beans
    5,267
    Distro
    Ubuntu 6.10 Edgy

    Re: [SOLVED] [Python] Single Line Output for the 'for' loop

    Using single print function, and joined arguments, is also slightly more effective:

    1) joining list uses less memory reallocation (especially compared to += accumulation)
    2) uses just single I/O call.

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •