function that modifies a string

placid Bulkan at gmail.com
Mon Jul 10 00:31:00 EDT 2006


greenflame wrote:
> I want to make a function that does the following. I will call it
> thefunc for short.
>
> >>> s = "Char"
> >>> thefunc(s)
> >>> s
> '||Char>>'
>
> I tried the following
>
> def thefunc(s):
>     s = "||" + s + ">>"
>
> The problem is that if I look at the string after I apply the function
> to it, it is not modified. I realized that I am having issues with the
> scope of the variables. The string in the function, s, is local to the
> function and thus I am not changing the string that was inputed, but a
> copy. I cannot seem to figure out how to get what I want done. Thank
> you for your time.

quick hack

def thefunc(s):
    return s = "||" + s + ">>"

>>> s = "hello"
>>> s = thefunc(s)
>>> print s
       ||hello>>


--Cheers




More information about the Python-list mailing list