I have the following bit of code:
def test():
fragment = ''
fragment = raw_input('Enter input')
while fragment not in string.ascii_letters:
fragment = raw_input('Invalid character entered, try again: ')
fragment.upper()
print fragment*3
However when I run it, say for an input value of p, fragment gets printed as 'ppp' - all lower case, i.e. the fragment.upper() line does not run. The same thing happens if I replace that line with string.upper(fragment) (and adding import string at the beginning). Can someone tell me what I'm doing wrong?
Strings are immutable. So functions like str.upper() will not modify str but return a new string.
>>> name = "xyz"
>>> name.upper()
'XYZ'
>>> print name
xyz # Notice that it's still in lower case.
>>> name_upper = name.upper()
>>> print name_upper
XYZ
So instead of fragment.upper() in your code, you need to do new_variable = fragment.upper()and then use this new_variable.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With