Skip to content
Advertisement

How can I edit this code so that it prints the result a certain way?

I’m trying to get this code:

line = input("String: ")
letters = ""
words = line.split()
for word in words:
    letters = letters + word[0]
print(" ".join(letters))

String = I have a dog
Result: I h a d

To print the result like this:

I
h
a
d

Advertisement

Answer

change your print statement for this one

print("n".join(letters))

n is the new line character, that’s why.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement