Click here to Skip to main content
15,887,302 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
def solve(s):
    if s.isalnum():
        Empty= s.split()
        Empty2= ""
        for string in Empty:
            if isinstance(string, str):
                if any(char.isdigit() for char in string):
                    Empty2 += string.lower()
                    Empty2 += " "
                else:
                    Empty2+= string.capitalize()
                    Empty2+= " "
        return Empty2
    else:
        return s.title()


What I have tried:

Here I have tried input of "z 3g" , the output appeared as: "Z 3G" , 'Z' is capitalized and this is good. But why "3g" became "3G" and this part should not make this:
if any(char.isdigit() for char in string):
    Empty2 += string.lower()
    Empty2 += " "

so how "3g" is capitalized despite of writing this part of code ????
Posted
Updated 10-Jul-23 18:54pm

1 solution

The space is not counted as alphanumeric, so your first test fails.
So what is returned is your string in title case. If you enter the input "zh 3gh" you will see that: it will return "Zh 3Gh" - title case.

30 seconds with a debugger would have shown you exactly what was going on, and saved you the 5 hours waiting for a reply ...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900