.upper Not Working In Python
Solution 1:
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 inrange(num_lines):
line = input("{}. Enter a word: ".format(i + 1))
originals.append(line)
alternating.append("".join([x.lower() if j % 2 == 0else x.upper() for j, x inenumerate(line)]))
print("Originals:\t", originals)
print("Alternating:\t", alternating)
With the following sample output:
Howmanywordsdoyouwanttoenter?: 31. Enteraword: Spam2. Enteraword: ham3. Enteraword: EGGSOriginals: ['Spam', 'ham', 'EGGS']Alternating: ['sPaM', 'hAm', 'eGgS']
Post a Comment for ".upper Not Working In Python"