2013/3/13 Jiewei Huang <jiewe...@gmail.com>: > Hi all, > > I'm currently stuck at this question on > > Writing a function len_str that takes a string as an argument and returns a > pair consisting of the length of the string and the string itself. > > Example: len_str('Meaning of life') should return the tuple (15, 'Meaning of > life'). > > > I can only think of this : > > len_str = ('welcome to life' ) > > print (len(len_str,), len_str) > > > However that not an correct answer I need to make a def len_str but I can't > seen to get it right. > -- > http://mail.python.org/mailman/listinfo/python-list
Hi, unless you are required to code the length-counting by hand as a part of the exercise, you would simply use the built-in function for that, i.e. http://docs.python.org/3.3/library/functions.html#len Tuples are created using the coma delimiter; optionally with enclosing parens. http://docs.python.org/3.3/library/stdtypes.html#tuples >>> input_string = "Meaning of life" >>> input_string 'Meaning of life' >>> len(input_string) 15 >>> (len(input_string), input_string) (15, 'Meaning of life') >>> Now you have to put the needed code to the function body; see http://docs.python.org/3.3/tutorial/controlflow.html#defining-functions (Be sure not to forget the "return" statement containing the result of your function.) hth, vbr -- http://mail.python.org/mailman/listinfo/python-list