Search code examples
pythonpython-3.xstringfunctioncapitalize

Function keeps executing else statement in Python


def capitalize(string, *restToLower):
    if (restToLower == True):
        print(string.capitalize())
                    
    else: print(string[0].upper() + string[1:])

OR

def capitalize(string, *restToLower):
    if (restToLower == True):
        print(string.capitalize())
                    
    print(string[0].upper() + string[1:])

The above mentioned function always running else statement.

I created this function to take a string and convert it into capitalize format.

What I want

if capitalize('apple') - "Apple"

and if capitalize('aPPle') - "APPle"

and if capitalize('aPPle', restToLower = True') or capitalize('aPPle', True) - "Apple"


Solution

  • You can try this one but always you have to put the boolean True or False.

    def capitalize(string, restToLower: bool):
        if restToLower == True:
            return string.capitalize()
        else:
            return string[0].upper() + string[1:]
    
    
    var = capitalize('aPPle', True)
    print(var)