Printing lists in python without spaces

前端 未结 7 758
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-12 16:47

I am doing a program that changes a number in base 10 to base 7, so i did this :

num = int(raw_input(\"\"))
mod = int(0)
list = []
while num> 0:
    mod =         


        
7条回答
  •  我在风中等你
    2021-01-12 17:40

    Take a look at sys.stdout. It's a file object, wrapping standard output. As every file it has write method, which takes string, and puts it directly to STDOUT. It also doesn't alter nor add any characters on it's own, so it's handy when you need to fully control your output.

    >>> import sys
    >>> for n in range(8):
    ...     sys.stdout.write(str(n))
    01234567>>> 
    

    Note two things

    • you have to pass string to the function.
    • you don't get newline after printing.

    Also, it's handy to know that the construct you used:

    for i in range (0,len(list)):
       print list[i],
    

    is equivalent to (frankly a bit more efficient):

    for i in list:
        print i,
    

提交回复
热议问题