简体   繁体   中英

I'm new to python and I am trying to figure out how to print letters one at a time on the same line

This isn't my actual code but it is just an example. How do I make it my code wait but stay on the same line?

    import pygame
    pygame.init()

    print("Hi")
    pygame.time.wait(1000)
    print(".")
    pygame.time.wait(500)
    print(".")
    pygame.time.wait(500)
    print(".")

The default end to a print() statement in python is '\\n' (new line). As @sj95126 mentions in his comment, you will want to replace this ending with an empty string. Fortunately, Python gives a simple way of doing this:

print("Hi", end="")
pygame.time.wait(1000)
print(".", end="")
pygame.time.wait(500)
print(".", end="")
pygame.time.wait(500)
print(".")

The above code will print:

Hi...

Similarly, if you wanted to make every line end in a comma, you could do:

print("A", end=",")
pygame.time.wait(1000)
print("B", end=",")
pygame.time.wait(500)
print("C", end=",")
pygame.time.wait(500)
print("Bye")

Which would print:

A,B,C,Bye

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM