How to Print One Character at a Time on the Same Line in Python?

Estimated read time 1 min read

To print one character at a time on the same line in Python, you can use the end parameter of the print() function. By default, the end parameter is set to "\n", which means that the print() function will add a new line character at the end of the output. However, you can change the value of the end parameter to an empty string ("") to prevent this and keep the output on the same line. Here’s an example:

string = "hello"

for char in string:
    print(char, end="")

This will print each character of the string on the same line:

hello

Note that the end parameter specifies what should be printed at the end of the line, so if you want to add a space between each character, for example, you can set end to a space:

string = "hello"

for char in string:
    print(char, end=" ")

This will print each character of the string on the same line, separated by a space:

h e l l o

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply