.upper not working in python

2024/11/17 8:36:30

I currently have this code

num_lines = int(input())
lines = []
tempy = ''
ctr = 1
abc = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z'
}
for i in range(0, num_lines):tempy = input()lines.append([])lines[i].append(tempy)for o in range(0, num_lines):for p  in range(0, len(lines[o])):for u in range(0, len(lines[o][p])):if lines[o][p][u] in abc:lines = str(lines)if ctr % 2 == 0:print(lines[o][p][u])lines[o][p][u].upper()else:print(lines[o][p][u])lines[o][p][u].lower()ctr += 1print(lines)

but the .upper line does not seem to have effect, can anyone please tell me why?

Thank you in advance, and if there is an answer already please kindly tell me that instead of marking as a duplicate cause I did search for a good hour

Answer

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']
https://en.xdnf.cn/q/120226.html

Related Q&A

Python SKlearn fit method not working

Im working on a project using Python(3.6) and Sklearn.I have done classifications but when I try to apply it for reshaping in order to use it with fit method of sklearn it returns an error. Heres what …

extracting n grams from huge text

For example we have following text:"Spark is a framework for writing fast, distributed programs. Sparksolves similar problems as Hadoop MapReduce does but with a fastin-memory approach and a clean…

Python: Input validate with string length

Ok so i need to ensure that a phone number length is correct. I came up with this but get a syntax error.phone = int(input("Please enter the customers Phone Number.")) if len(str(phone)) == 1…

Mergesort Python implementation

I have seen a lot of mergesort Python implementation and I came up with the following code. The general logic is working fine, but it is not returning the right results. How can I fix it? Code: def me…

Use variable in different class [duplicate]

This question already has answers here:How to access variables from different classes in tkinter?(2 answers)Closed 7 years ago.I am a beginner in python. I have a problem with using variable in differ…

Embedded function returns None

My function returns None. I have checked to make sure all the operations are correct, and that I have a return statement for each function.def parameter_function(principal, annual_interest_rate, durati…

calculate days between several dates in python

I have a file with a thousand lines. Theres 12 different dates in a single row. Im looking for two conditions. First: It should analyze row by row. For every row, it should check only for the dates bet…

Appeding different list values to dictionary in python

I have three lists containing different pattern of values. This should append specific values only inside a single dictionary based on some if condition.I have tried the following way to do so but i go…

Split only part of list in python

I have a list[Paris, 458 boulevard Saint-Germain, Marseille, 29 rue Camille Desmoulins, Marseille, 1 chemin des Aubagnens]i want split after keyword "boulevard, rue, chemin" like in output[Sa…

How to find the index of the element in a list that first appears in another given list?

a = [3, 4, 2, 1, 7, 6, 5] b = [4, 6]The answer should be 1. Because in a, 4 appears first in list b, and its index is 1.The question is that is there any fast code in python to achieve this?PS: Actual…