How to Print Each Letter of a String on a New Line in Python?

Estimated read time 1 min read

To print each letter of a string on a new line in Python, you can use a for loop and the print() function. Here’s an example:

my_string = "Hello, World!"

for letter in my_string:
    print(letter)

In this example, we define a string my_string. We then use a for loop to iterate over each character in the string, and we print each character on a new line using the print() function.

The output of the above code will be:

H
e
l
l
o
,
 
W
o
r
l
d
!

Note that the print() function automatically adds a newline character after each call, so each letter is printed on a new line. If you want to print the letters without a newline character, you can use the end parameter of the print() function to specify an empty string, like this:

my_string = "Hello, World!"

for letter in my_string:
    print(letter, end='')

This will print the letters on a single line, without any spaces or newlines between them.

You May Also Like

More From Author

+ There are no comments

Add yours

Leave a Reply