H
H
Hikikomori912015-10-24 21:02:33
Python
Hikikomori91, 2015-10-24 21:02:33

Why doesn't the first character change?

Good evening. I'm learning Python, I have code:

def test_funct(*args):
    args_ = []
    for i in range(len(args)):
        if i == 0:
            args_.append([str(elem.lower().replace(elem[0], elem[0].upper(), 1)) for elem in (str(args[i]).split())])
    return args_

print (test_funct("Ya LOMAL steklo"))

As planned, this code should turn the input data into a sheet broken by spaces, and reduce each element of this sheet to lower case, and then raise the first character to upper case. I get this as an output:

When should this happen:

Why is this happening? I can make character replays a separate function after the sheet is created, but that's an extra line. How can I change the current code without adding new features?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vitaly, 2015-10-24
@Hikikomori91

Why not really break it down? At you even more than 80 characters the line turns out. It's not pep8 anymore, it's hard to read - and all that.
Well, as for the question. It turns out that replace takes the first character from the source string, and then looks for it in the one in which all the characters have been converted to lowercase and, of course, does not find it there in the first two cases.
Add lower to the first argument of replace
or use slicing altogether,
and Python has a built-in title() method for strings that does pretty much the same thing.

V
Vladimir Martyanov, 2015-10-24
@vilgeforce

Breaking your main line into several simpler and more understandable ones and using a debugger/output intermediate results should help.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question