Why is print() not working anymore with python?

后端 未结 3 725
清歌不尽
清歌不尽 2021-01-28 03:29

in fact this should not be a problem because it\'s fairly basic. I just want to print out an array of categories, but in one line and separated by comma.

for ent         


        
3条回答
  •  半阙折子戏
    2021-01-28 03:40

    Printing to the terminal in Python 3 is usually "line buffered". That is, the whole line is only printed to the terminal once a newline character is encountered.

    To resolve this problem you should either print a new line at the end of the for loop, or flush stdout.

    eg.

    for entry in categories:
        print(entry, ", ", end='')
    print(end='', flush=True) # or just print() if you're not fussed about a newline
    

    However, there are better ways of printing an array out. eg.

    print(", ".join(str(entry) for entry in categories))
    # or
    print(*categories, sep=", ")
    

提交回复
热议问题