Assign a print statement to a variable in a function in Python 2.7

前端 未结 3 909
生来不讨喜
生来不讨喜 2021-01-29 12:38

I\'m trying to assign a print statement to a variable in a function:

def namer(fn, ln=\'Smith\'):
    # return value, default value
    r = print \"Your name is          


        
3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-29 13:11

    The print statement only prints the value to your monitor. It doesn't make any sense to assign it to a variable.

    If your code is meant to store the string in the variable r, then:

    r = "Your name is " + fn + " "+ ln

    This would work provided fn is a string, if not:

    r= "Your name is " + str(fn) +" "+ ln

提交回复
热议问题