Home > front end >  How to convert a for loop output into a simple variable?
How to convert a for loop output into a simple variable?

Time:11-23

I will Show you a simple demonstration of what I want to accomplish:

import numpy as np
word = input("Enter a word : "  ' ')
pxlength = (len(word))
word= " ".join(word)
m = (np.str_(word))
for g in range(5):
     print(m)

if I Enter my name- Eitan it will print:

e i t a n
e i t a n
e i t a n
e i t a n
e i t a n

The thing that I want is a variable that represent the output,we will call him e, im trying to get to this: print(e) and it will print:

    e i t a n
    e i t a n
    e i t a n
    e i t a n
    e i t a n

I tried to look for this in so much places but I probably dont know how and what exactly to search.. I know that this is a very silly qu but I really need help, tnx.

CodePudding user response:

You can do something like that

e = f'{word}\n' * 5
print(e)

CodePudding user response:

What you are trying to achieve is more complicated than it seems and probably means the problem itself is flawed - you shouldn't need to use print output further in your code. That being said, it is possible to achieve by capturing stdout and stderr, as described here: https://stackoverflow.com/a/40417352/9296093

CodePudding user response:

try this:

word = input("Enter a word : "  ' ')
word_sep = ' '.join(word)
e = '\n'.join([word_sep]*5)
print(e)
  • Related