0
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"

Kunal Tanwar
  • 1,209
  • 1
  • 8
  • 23
  • 1
    If `restToLower` is meant to be a boolean, why are you using `**restToLower`? – not_speshal Jul 28 '21 at 18:35
  • 1
    In your own words, why do you think `restToLower` should ever be equal to `True`? What happened when you tried displaying the value of `restToLower`? Is it a boolean? Can a dict ever be equal to `True`? In your own words, when you write `def capitalize(string, **restToLower):`, what *exactly* do you think the `**` does? – Karl Knechtel Jul 28 '21 at 18:35
  • 2
    I think you need to understand *args **kwargs. [This](https://stackoverflow.com/questions/36901/what-does-double-star-asterisk-and-star-asterisk-do-for-parameters) might help. – not_speshal Jul 28 '21 at 18:36
  • It seems like your intention is for `restToLower` to be a single parameter with a default value. What happened when you tried putting `python default value` into a search engine? Did you see examples that show how to give a default value for a parameter? Do they look like your code? – Karl Knechtel Jul 28 '21 at 18:36
  • @not_speshal I want it to be an optional argument I add double ** by mistake! it should be single *. – Kunal Tanwar Jul 28 '21 at 18:38
  • @KarlKnechtel well it will make it a keyword argument. – Kunal Tanwar Jul 28 '21 at 18:39
  • @KarlKnechtel well I want while calling the function if we do not add ```restToLower = True``` it will just convert the first alphabet of string to capital and if there are more capitals it will not conflict with them. Like I mentioned ```capitalize('aPPle')``` = "APPle". – Kunal Tanwar Jul 28 '21 at 18:42

2 Answers2

0

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)
  • You can make it like I want by just adding ```def capitalize(string, restToLower : bool = False):```. By the way thanks man!!!! – Kunal Tanwar Jul 29 '21 at 13:19
-1

That's not how you provide default values for arguments. You need to do def capitalize(string, restToLower=False) if you want it to be False by default. The way you've defined it, restToLower is a tuple containing any number of arguments you enter after the first, so it will never be equal to True.

As an experiment:

def capitalize(string, *restToLower):
    print(restToLower)

capitalize("abcd")              # ()
capitalize("abcd", True)        # (True,)
capitalize("abcd", True, False) # (True, False)
capitalize("abcd", True, 1234)  # (True, 1234)
Pranav Hosangadi
  • 23,755
  • 7
  • 44
  • 70