[Solved] .upper not working in python


The .upper() and .lower() functions do not modify the original str. Per the documentation,

For .upper():

str.upper()

Return a copy of the string with all the cased characters converted to uppercase.

For .lower():

str.lower()

Return a copy of the string with all the cased characters converted to lowercase.

So if you want the uppercase and lowercase characters respectively, you need to print lines[o][p][u].upper() and lines[o][p][u].lower() as opposed to lines[o][p][u]. However, if I correctly understand your objective, from your code sample, it looks as though you’re trying to alternate uppercase and lowercase characters from string inputs. You can do this much more easily using list comprehension with something like the following:

num_lines   = int(input("How many words do you want to enter?: "))
originals   = []
alternating = []

for i in range(num_lines):
        
    line = input("{}. Enter a word: ".format(i + 1))
    originals.append(line)
    alternating.append("".join([x.lower() if j % 2 == 0 else x.upper() for j, x in enumerate(line)]))

print("Originals:\t",   originals)
print("Alternating:\t", alternating)

With the following sample output:

How many words do you want to enter?: 3
1. Enter a word: Spam
2. Enter a word: ham
3. Enter a word: EGGS
Originals:       ['Spam', 'ham', 'EGGS']
Alternating:     ['sPaM', 'hAm', 'eGgS']

1

solved .upper not working in python