-5

I saw this code on YouTube. The code should turn all lower case strings (ab ,cd) into upper case string but when I tried this code the output was the same as the array without change. I want know what is going on behind the scenes.

  x = ['ab', 'cd'] 

    for i in x: 
    i.upper()
    print(i)
py.flow
  • 3
  • 2

3 Answers3

1

upper() returns the uppercase of the string it's called on, but does not modify that string. So you're calling upper(), but then ignoring its return value.

You could capture the return value in a variable and then print it:

for i in x:
    u = i.upper()
    print(u)

Or just print it directly:

for i in x:
    print(i.upper())
Mureinik
  • 297,002
  • 52
  • 306
  • 350
1

Here. I hope this works!

x = ['ab', 'cd'] # Your array
uppercase_string = str(x).upper() # makes uppercase
print(uppercase_string) # prints uppercase

You don't need to make a for loop to print an array. Plus posting an image makes it harder for answers to be made.

CodeShed
  • 13
  • 3
  • This is a good approach, but note that the result is a string; if i understand the question, they might want the elements in the list modified. – rv.kvetch Oct 26 '21 at 13:12
0

You can put print(i.upper()) instead of just print.

That didn't work because, i.upper() returns the string back after converting all the letters to uppercase, thus you can either use an assignment statement to keep hold of it or print it out like the one mentioned above.

MK14
  • 481
  • 3
  • 9