def count_letter(string,letter):
count = 0
for i in string:
if i == letter:
count = count + 1
return count
If we accidentally passed the letter first, followed by the string, the
function would simply return a zero, unless the letter and the string were identical.
To reduce the necessity to know the order of arguments in a function, Python allows
named arguments. When you call a function, you can precede some or all of the
arguments by a name and an equal sign, to let the function know exactly which argument
you are passing to the function. So if we invoke the count_letter function as
num = count_letter(letter='a',string='dead parrot')
we will get the correct answer (2) even though the arguments are in a
different order than they were defined.
To provide even greater flexibility, when you define a function Python allows you to specify a default value for any or all of the arguments to that function; the user then need only supply those arguments for which there are no defaults, and specifying the arguments with defaults in the function definition becomes optional. To supply a default value when defining a function, you use a syntax similar to the way you use named arguments when calling a function, namely following the argument name with an equal sign and the desired default value. Using the count_letter function as an example, suppose we wish, by default, to count the number of blanks in a string. We would redefine the function as follows:
def count_letter(string,letter=' '):
count = 0
for i in string:
if i == letter:
count = count + 1
return count
The specification of letter=' ' tells Python that when the
count_letter function is called, and no value is given for the letter
argument, to use a blank as the value for letter. Keep in mind that if a
value is given for letter, either as a named argument or by calling
the function with two arguments, this default will be overridden. Thus all the
following calls to the function will count the number of blanks in the specified
string:
mystring = 'this parrot is dead' count_letter(mystring,' ') count_letter(mystring) count_letter(letter=' ',string=mystring)
When you mix named and unnamed arguments in a function call, the unnamed arguments must come before the named ones in the argument list. Thus, when writing a function, you should put required arguments to the function in the argument list before the ones that you consider optional.
Suppose we wish to extend the count_letter program by providing it with an additional argument representing a character to which the letter we're searching for should be changed. Since strings are immutable, we can't change the string directly; we need to convert it to a list, and then reconstruct the string from the list elements. We'll call the function change_letter, and make its default behavior changing blanks to empty strings, i.e. removing blanks from a string:
def change_letter(string,frm=' ',to=''):
newstring = ''
for i in string:
if i == frm:
newstring = newstring + to
else:
newstring = newstring + i
return newstring
Note that we could not use the name ``from'' as an
argument to the function, since it is a reserved word in Python. Having
written the function, we could, for example, change blanks to plus signs
with the following call:
>>> change_letter('string with blanks',to='+')
'string+with+blanks'
However, trying to call the function with a named argument before
the unnamed argument will result in an error:
>>> change_letter(frm='b','string with blanks') SyntaxError: non-keyword arg after keyword argIf we wanted to put the named argument first, we'd need to supply a name for all the arguments which follow it:
>>> change_letter(frm='b',string='string with blanks') 'string with lanks' >>>