Swapping uppercase and lowercase in a string

2019-06-22 07:13发布

I would like to change the chars of a string from lowercase to uppercase.

My code is below, the output I get with my code is a; could you please tell me where I am wrong and explain why? Thanks in advance

test = "AltERNating"

def to_alternating_case(string):
    words = list(string)
    for word in words:
        if word.isupper() == True:
            return word.lower()
        else:
            return word.upper()  

print to_alternating_case(test)

9条回答
冷血范
2楼-- · 2019-06-22 07:36

If you want to invert the case of that string, try this:

>>> 'AltERNating'.swapcase()
'aLTernATING'
查看更多
forever°为你锁心
3楼-- · 2019-06-22 07:37
string=input("enter string:")
temp=''
ss=list(string)
for item in range(len(ss)):
    if item%2==0:
        temp+=ss[item].lower()
    else:
        temp+=ss[item].upper()
print(temp)
查看更多
三岁会撩人
4楼-- · 2019-06-22 07:39

Here is a short form of the hard way:

alt_case = lambda s : ''.join([c.upper() if c.islower() else c.lower() for c in s])
print(alt_case('AltERNating'))

As I was looking for a solution making a all upper or all lower string alternating case, here is a solution to this problem:

alt_case = lambda s : ''.join([c.upper() if i%2 == 0 else c.lower() for i, c in enumerate(s)])
print(alt_case('alternating'))
查看更多
老娘就宠你
5楼-- · 2019-06-22 07:43

You are returning the first alphabet after looping over the word alternating which is not what you are expecting. There are some suggestions to directly loop over the string rather than converting it to a list, and expression if <variable-name> == True can be directly simplified to if <variable-name>. Answer with modifications as follows:

test = "AltERNating"

def to_alternating_case(string):
    result = ''
    for word in string:
        if word.isupper():
            result += word.lower()
        else:
            result += word.upper()
    return result

print to_alternating_case(test)

OR using list comprehension :

def to_alternating_case(string):
    result =[word.lower() if word.isupper() else word.upper() for word in string]
    return ''.join(result)

OR using map, lambda:

def to_alternating_case(string):
    result = map(lambda word:word.lower() if word.isupper() else word.upper(), string)
    return ''.join(result)
查看更多
啃猪蹄的小仙女
6楼-- · 2019-06-22 07:44

Your loop iterates over the characters in the input string. It then returns from the very first iteration. Thus, you always get a 1-char return value.

test = "AltERNating"

def to_alternating_case(string):
    words = list(string)
    rval = ''
    for c in words:
        if word.isupper():
            rval += c.lower()
        else:
            rval += c.upper()
    return rval    

print to_alternating_case(test)
查看更多
相关推荐>>
7楼-- · 2019-06-22 07:45

That's because your function returns the first character only. I mean return keyword breaks your for loop.

Also, note that is unnecessary to convert the string into a list by running words = list(string) because you can iterate over a string just as you did with the list.

If you're looking for an algorithmic solution instead of the swapcase() then modify your method this way instead:

test = "AltERNating"

def to_alternating_case(string):
    res = ""
    for word in string:
        if word.isupper() == True:
            res = res + word.lower()
        else:
            res = res + word.upper()
    return res


print to_alternating_case(test)
查看更多
登录 后发表回答