连接字符串?

ali*_*cew 6 python string

def change(s):
    s = s + "!"


word = "holiday"
change(word)    
print word
Run Code Online (Sandbox Code Playgroud)

为什么输出这个是"假日"而不是"假日"?是因为3行代码不属于函数吗?如果是这样,为什么它会在函数之外,因为它是在更改函数之后?

Ósc*_*pez 6

试试这个:

def change(s):
    return s + "!"
Run Code Online (Sandbox Code Playgroud)

像这样使用它:

word = 'holiday'
print change(word)
Run Code Online (Sandbox Code Playgroud)

或者像这样:

word = 'holiday'
word = change(word)
print word
Run Code Online (Sandbox Code Playgroud)

请注意,对函数内部的参数字符串所做的任何修改都不会在函数外部看到,因为函数中的s参数是函数范围的本地参数,以及您执行的任何更改(例如问题中的代码) )只会在函数内部显示,而不是在外部.Python中的所有函数参数都按值传递.