1

Alright so I've been trying to make this program and part of it requires me to make a string uppercase. It wasn't working so I just went into a new file and typed 3 lines and it still didn't work. Can someone tell me why this is not working?

word = 'armadillo'
word.upper()
print(word)

prints armadillo, no caps.

Austin Whitelaw
  • 107
  • 1
  • 11

3 Answers3

2

You are not changing the content of word but temporarily making it uppercase. To change words value to all uppercase you must assign it to itself in uppercase form.

word = 'armadillo'
word = word.upper()
print(word)

Try using this example and you will see you get the results you expected.

Ryan
  • 1,972
  • 2
  • 23
  • 36
0

You need to assign word.upper() to a variable. word.upper() does not modify word, so you need to do something like word = word.upper().

El'endia Starman
  • 2,204
  • 21
  • 35
  • Ah ok thanks, I have been messing around but just simply could not figure it out. I guess I used it in the interpreter before so I thought that it worked fine. – Austin Whitelaw Nov 26 '15 at 02:47
0

First, python string object is immutable, which means cannot be changed so that you should create new string object if you want change its attribute(status).

You want to change status of the string object, then you should create new string object and it's what upper() does.

word.upper() does not change 'word' itself but returns new string.

This should work:

word = 'armadillo'
uppercase_word = word.upper()
print(uppercase_word)
yslee
  • 236
  • 1
  • 12