Path: csiph.com!usenet.pasdenom.info!gegeweb.org!usenet-fr.net!nerim.net!novso.com!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.010 X-Spam-Evidence: '*H*': 0.98; '*S*': 0.00; 'example:': 0.03; 'argument': 0.04; 'subject:2.7': 0.09; 'subject:string': 0.09; 'tuple': 0.09; 'def': 0.10; 'itself.': 0.11; 'subject:python': 0.11; 'body;': 0.16; 'function.)': 0.16; 'optionally': 0.16; 'string': 0.17; '>>>': 0.18; 'all,': 0.21; 'tuples': 0.22; 'statement': 0.23; 'header:In-Reply-To:1': 0.25; 'right.': 0.27; 'question': 0.27; 'i.e.': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'consisting': 0.29; 'url:mailman': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'url:python': 0.32; 'print': 0.32; 'url:listinfo': 0.32; 'to:addr:python-list': 0.33; 'hi,': 0.33; 'that,': 0.34; "can't": 0.34; 'received:google.com': 0.34; 'needed': 0.35; 'built-in': 0.35; 'received:209.85': 0.35; 'created': 0.36; 'but': 0.36; 'url:org': 0.36; 'url:library': 0.36; 'should': 0.36; 'subject: (': 0.36; 'received:209': 0.37; 'received:209.85.216': 0.37; 'subject:: ': 0.38; 'url:docs': 0.38; 'sure': 0.38; 'to:addr:python.org': 0.39; 'takes': 0.39; 'url:mail': 0.40; 'think': 0.40; 'your': 0.60; 'containing': 0.61; 'stuck': 0.65; 'hand': 0.82; 'exercise,': 0.84; 'url:functions': 0.84; 'url:tutorial': 0.93 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:x-received:in-reply-to:references:date:message-id :subject:from:to:content-type; bh=K4z2Msdkotmnm0smnUWuMrEyEtrLOu91DeN0TGlEZvk=; b=y1UM88lVUEqDMsd++3cmkdq2ZT32Aeuc7dDtDqd42l1JZB9IxpTgYHYVXLYdoWh5Jv KFv4g/4Nyal3GK2Md7eSh3yAgbXkosCtAqzJbRpFd9qSBU6UUz+0IW3wwtUhtuW1V0lm SNUlWU1IAsM+XqGfUOmKVEQrjxMuYGDPw9bk2qq/s9LjJn/ls8J3meuQJ7I5fFC9aYRd VivfNch1OIVdFYOlhIOMeGkQr4svmoIsOWRDwxS8PMuDbqjwblHbaud1grtyoV/wbOzU M3TpnvzyUvoVlb8zjZn85sDlo9zysflTo+ws+WmNPzuyLX0u7hZt2pEOdLprrCCBpuEw FEFg== MIME-Version: 1.0 X-Received: by 10.229.78.80 with SMTP id j16mr6000971qck.87.1363136819976; Tue, 12 Mar 2013 18:06:59 -0700 (PDT) In-Reply-To: <36706e17-0a2d-46eb-b222-09b90ec6ab58@googlegroups.com> References: <36706e17-0a2d-46eb-b222-09b90ec6ab58@googlegroups.com> Date: Wed, 13 Mar 2013 02:06:59 +0100 Subject: Re: A string and an integer to appear in tuple (python 2.7) From: Vlastimil Brom To: python-list@python.org Content-Type: text/plain; charset=ISO-8859-1 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Newsgroups: comp.lang.python Message-ID: Lines: 48 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1363136823 news.xs4all.nl 6858 [2001:888:2000:d::a6]:38188 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:41158 2013/3/13 Jiewei Huang : > 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