0

I like to save the two print statements in 2 different variables. How can i do this?

 with open(file_to_open) as f: for line in f: # split the line line = line.strip() columns = line.split(",") if columns[0] == "1": print(line, end='') if columns[0] == "2": print(line, end='') 
2
  • 3
    save the string that is being printed. print itself is just a function, that displays stuff. Commented Apr 11, 2019 at 9:02
  • 5
    This question makes no sense. Saying "I want to store a statement in a variable" is like saying "I want to store my emotions in this tupperware container". Those two things are not compatible. Commented Apr 11, 2019 at 9:04

2 Answers 2

2

The print(x) function implicitly

  1. calls str(x)
  2. displays it
  3. returns None

So, you can't do

 stored = print(x) 

Instead, write

stored_value = str(x) 
Sign up to request clarification or add additional context in comments.

Comments

0
with open(file_to_open) as f: for line in f: # split the line line = line.strip() columns = line.split(",") if columns[0] == "1": def af(line=line): print(line, end='') a = af if columns[0] == "2": def bf(line=line): print(line, end='') b= bf 

Using closure, you can save the print statement and its parameter at the time it called. Then you can call this saved statement at any time you want later.

a() b() 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.