0

The following code:

def function(X):
    X.upper()
    if X == 'YES':
        print ('success')
    else:
        print ('fail')
function('yes')

Produces:

fail

But this code:

def function2(X):
    Y = X.upper()
    if Y == 'YES':
        print ('success')
    else:
        print ('fail')
function2('yes')

Gives me:

success

Why is this? I want to be able to edit my input variables within my functions. Is there a more efficient way to do this than copying variable values to new variables? I'm running Python 3.7.1.

Thanks!

Matthew Snyder
  • 383
  • 2
  • 11

1 Answers1

3

Because "".upper() returns new string, it doesn't change the original. Strings are immutable in Python.

marke
  • 1,024
  • 7
  • 20