0

I want to be able to print the whole variable, or just detect it. If I run it through a for loop as I've done in the code below, it just prints the individual letters. How can it print the whole word?

Question = input()
Answer = input()

def test():
    for words in Answer:
        print(words)


test()
Alexhs
  • 1

4 Answers4

0

Since answer is a string, and a string is an enumerable type, it enumerates over the characters. If you want to print a plain string just do print(Answer).

jxramos
  • 7,356
  • 6
  • 57
  • 105
0

This would work perfectly

Question = input()
Answer = input()

def test():
    for words in Answer.split(' '):
        print(words)

test()
Mohil Patel
  • 437
  • 3
  • 9
0

When you are iterating a string in python what it does, it makes an iterable out of a string and then it print's each letter in a given word. This is for both single word and multiple words, so either you can just print(Answer) or you can first split it by e.g. space and then print each word in a sentence like this:

answer = Answer.split()  # splits by space, you can provide any separator and this will generate a list for you
for word in answer:
    print(word)

This will print each word even if it's only 1.

simkusr
  • 770
  • 2
  • 11
  • 20
0

This is a poorly phrased question and poorly researched question which can get you banned from asking questions in Stack overflow so keep that in mind when you post another question.

You can do

for word in Answer.split(" "):
    print(word)

If that's not what you are looking for you can try-

print(Answer)
ani
  • 303
  • 3
  • 15