Home > other >  "Replace capital letters in a string with "_lower case letter" and print the output&q
"Replace capital letters in a string with "_lower case letter" and print the output&q

Time:02-16

For some reason following code does not work. The first part of if statement seems to work. Any ideas on what is the problem with the code?

sentence_input = str(input())
a = "_"
string = ""
if sentence_input.islower():
    print(sentence_input)
else:
    character_list = list(sentence_input)
    for i in character_list:
        if i.isupper():
            i.replace(i, a   i.lower())
    print(string.join(character_list))

CodePudding user response:

In the else statement, you seem to be trying to replace the character with the new character. What you should actually be doing is replacing the element in the array altogether

try this:

sentence_input = str(input())
a = "_"
string = ""
if sentence_input.islower():
    print(sentence_input)
else:
    character_list = list(sentence_input)

    for i, c in enumerate(character_list):
        if c.isupper():
            character_list[i] = "_"   c.lower()
    print(string.join(character_list))
  • Related