[Tutor] Help Passing Variables
Prasad, Ramit
ramit.prasad at jpmorgan.com
Mon Oct 29 17:12:50 CET 2012
David Hutto wrote:
> #A little more complex in terms of params:
>
> def SwapCaseAndCenter(*kwargs):
>
> if upper_or_lower == "upper":
> print a_string.center(center_num).upper()
>
> if upper_or_lower == "lower":
> print a_string.center(center_num).lower()
>
> a_string = raw_input("Give me a word, or letter: ")
> upper_or_lower = raw_input("upper, or lower character(s): ")
> center_num = int(raw_input("Where should number be centered?: "))
> SwapCaseAndCenter(a_string, upper_or_lower, center_num)
>
>
This function is using the global variables for each of
its variables. To use the versions passed in you need
to define the names from kwargs; the name "kwargs" is
usually used in reference to a keyword arguments which
should be denoted by double asterisk instead of a single one.
In this case kwargs is confusingly a positional list of arguments
(sometimes referred to as "args" instead).
For a function called SwapCaseAndCenter, I see it doing
no case "swapping" unless it refers to changing the entire
string to a specific case? At the very least, no centering
is being done.
To format I use string formatting[0] which has its own
syntax[1].
>>> '{0:^40}'.format( 'test' ) # The below line is the repr
' test '
>>> '{1:^{0}}'.format( 40, 'test' ) # Dynamically generate width
' test '
>>> '{0:^{1}}'.format( 'test', 40 )
' test '
[0]: http://docs.python.org/2/library/string.html#formatstrings
[1]: http://docs.python.org/2/library/string.html#formatspec
Ramit Prasad
This email is confidential and subject to important disclaimers and
conditions including on offers for the purchase or sale of
securities, accuracy and completeness of information, viruses,
confidentiality, legal privilege, and legal entity disclaimers,
available at http://www.jpmorgan.com/pages/disclosures/email.
More information about the Tutor
mailing list