I can't get the characters to all be printed on one line.
list = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for item in list:
sleep(0.25)
print(item)
i tried using a list and imported the sleep library
I can't get the characters to all be printed on one line.
list = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for item in list:
sleep(0.25)
print(item)
i tried using a list and imported the sleep library
By default, print()
ends each string with a newline character, but you can change this by providing your own ending string with the end=
keyword argument. In your case, you'll want to pass end=''
to print nothing at the end.
Also, by default, your terminal won't show any text unless there's a new line because of line buffering. To fix this, flush the output with flush=True
from time import sleep
lst = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for item in lst:
sleep(0.25)
print(item, end='', flush=True)
Did you know that you can iterate over strings? That means you don't need a list of chars:
import time
message = "Welcome to WORDLE"
for letter in message:
print(letter, end="", flush=True)
time.sleep(0.25)
print()
end=""
parameter tells print to place an empty string at the end, instead of the default '\n'
flush=True
parameter tells print to output the character right away instead of waiting for the internal buffer to get full before outputing.In your print, add this code after item as a second parameter : end=''
. By default, every time you printed, the default value \n
was added.
The print
function takes an end
argument, by default a newline. Change that to an empty string to get all letters on one line. You also need to add flush=True
so that Python doesn't buffer the output till the line is done.
from time import sleep
letters = ["W","e","l","c","o","m","e","","t","o","","w","o","r","d","l","e"]
for letter in letters:
sleep(0.25)
print(letter, end='', flush=True)
As pointed out by @Michael M., it's bad practice to name your variable e.g., "list", as this hides / shadows the official list keyword. I've renamed it to "letters" here, which makes it clearer what it is used for.