0

I write this code but it does not work:

a=input('word: ')
for i in a:
    print(i,'')

what is the problem?

1
  • 1
    What is your expected output? Commented May 20, 2020 at 9:27

4 Answers 4

2

By default, print inserts a linebreak at the end of the string. You have to change it:

a=input('word: ')
for i in a:
  print(i,end=' ')

Output for Input Hello:
H e l l o

Sign up to request clarification or add additional context in comments.

Comments

1

You should provide the expected output because now we have to assume what it is you are trying to achieve.

When using "abc" as input I am now assuming you want

a b c

instead of

a
b
c

The problem here is, with every loop you call the 'print' function which starts a new line automatically when you should format your output correctly before calling print. Try this:

a=input('word: ')
s = ""
for i in a:
    s += i + " "
print(s)

Comments

1

Try doing this

a=input('word: ')

def func(a):
    lst = list(a)

    return " ".join(lst)

print(func(a))

Comments

0

Actually you didn't add space. Try this:

a=input('word: ')
for i in a:
    print(i,' ')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.