python: printing horizontally rather than current default printing

前端 未结 10 2068
孤城傲影
孤城傲影 2021-02-05 06:32

I was wondering if we can print like row-wise in python.

Basically I have a loop which might go on million times and I am printing out some strategic counts in that loop

相关标签:
10条回答
  • 2021-02-05 06:56

    Use this code for your print

    print(x,end="")

    0 讨论(0)
  • 2021-02-05 06:59
    a=int(input("RangeFinal "))
    print("Prime Numbers in the range")
    for n in range(2, a):
       p=0
       for x in range(2, n):
            if n % x == 0:
                    break
            else:
               if(p==0):
                  print(n,end=' ')
                  p=1
    

    Answer

    RangeFinal 19
    Prime Numbers in the range
    3 5 7 9 11 13 15 17 
    
    0 讨论(0)
  • 2021-02-05 07:01

    If you add comma at the end it should work for you.

    >>> def test():
    ...    print 1,
    ...    print 2,
    ... 
    >>> test()
    1 2
    
    0 讨论(0)
  • 2021-02-05 07:03
    my_list = ['keyboard', 'mouse', 'led', 'monitor', 'headphones', 'dvd']
    for i in xrange(0, len(my_list), 4):
        print '\t'.join(my_list[i:i+4])
    
    0 讨论(0)
  • 2021-02-05 07:06

    In Python2:

    data = [3, 4]
    for x in data:
        print x,    # notice the comma at the end of the line
    

    or in Python3:

    for x in data:
        print(x, end=' ')
    

    prints

    3 4
    
    0 讨论(0)
  • 2021-02-05 07:09

    You can add a comma after your call to print to avoid the newline:

    print 3,
    print 4,
    # produces 3 4
    
    0 讨论(0)
提交回复
热议问题