Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #41157

Re: A string and an integer to appear in tuple (python 2.7)

Path csiph.com!usenet.pasdenom.info!aioe.org!news.stack.nl!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!newsgate.cistron.nl!newsgate.news.xs4all.nl!post.news.xs4all.nl!not-for-mail
Return-Path <oscar.j.benjamin@gmail.com>
X-Original-To python-list@python.org
Delivered-To python-list@mail.python.org
X-Spam-Status OK 0.001
X-Spam-Evidence '*H*': 1.00; '*S*': 0.00; 'example:': 0.03; 'argument': 0.04; 'argument:': 0.09; 'subject:2.7': 0.09; 'subject:string': 0.09; 'tuple': 0.09; 'cc:addr:python-list': 0.10; 'def': 0.10; 'itself.': 0.11; 'subject:python': 0.11; 'value.': 0.15; 'cc:name:python list': 0.16; 'multiplied': 0.16; 'string': 0.17; 'wrote:': 0.17; 'variable': 0.20; 'all,': 0.21; 'help.': 0.22; 'cc:2**0': 0.23; 'example': 0.23; 'cc:addr:python.org': 0.25; 'header:In-Reply-To:1': 0.25; 'right.': 0.27; 'question': 0.27; 'message-id:@mail.gmail.com': 0.27; 'correct': 0.28; 'consisting': 0.29; 'prints': 0.29; "i'm": 0.29; 'function': 0.30; 'code': 0.31; 'problem.': 0.32; 'print': 0.32; "can't": 0.34; 'received:google.com': 0.34; 'received:209.85.220': 0.35; 'received:209.85': 0.35; 'but': 0.36; 'wanted': 0.36; "didn't": 0.36; 'should': 0.36; 'subject: (': 0.36; 'received:209': 0.37; 'subject:: ': 0.38; 'apply': 0.39; 'takes': 0.39; 'called': 0.39; 'think': 0.40; 'your': 0.60; 'times': 0.63; 'stuck': 0.65; '2013': 0.84; 'oscar': 0.84
DKIM-Signature v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=x-received:mime-version:in-reply-to:references:from:date:message-id :subject:to:cc:content-type; bh=wI98Snqi30+B1e+YTDTylmyhkF6v5nNmBGwFt8z/JKk=; b=H82g8sZnjDJhuuKHvskd7zYKfGhzGu8hJ8vZ5EHs9IusmhqoSEPTAEbikYvaf7bXjN Ko1IsKkN89EMHozJhTSbygFulqV9Q5MJo6iBENB3Yfyf1EdM6oWL1IzHISBJTnvrc9Rh kHTzIIimuDnHtwYtecfgPV5Te5/aymPYHJsWUUBkgMvMMHCaoItS65dXxtyWXlAiodhF xXgmSq5OcIlG2+ciEJBHWAjzCnOwmdY0noO8l6nvJhxe4c3I3odnu9bc0YSZxp0yNp6G UEUgRG1xqZ2uMXc1zzbxOkUEBiRBsHhK4wCIOtFHSVqzZFLlRLcWQN0IB1zi+93HUkP+ xqaQ==
X-Received by 10.58.196.240 with SMTP id ip16mr7431469vec.50.1363135454486; Tue, 12 Mar 2013 17:44:14 -0700 (PDT)
MIME-Version 1.0
In-Reply-To <36706e17-0a2d-46eb-b222-09b90ec6ab58@googlegroups.com>
References <36706e17-0a2d-46eb-b222-09b90ec6ab58@googlegroups.com>
From Oscar Benjamin <oscar.j.benjamin@gmail.com>
Date Wed, 13 Mar 2013 00:43:54 +0000
Subject Re: A string and an integer to appear in tuple (python 2.7)
To Jiewei Huang <jiewei24@gmail.com>
Content-Type text/plain; charset=ISO-8859-1
Cc Python List <python-list@python.org>
X-BeenThere python-list@python.org
X-Mailman-Version 2.1.15
Precedence list
List-Id General discussion list for the Python programming language <python-list.python.org>
List-Unsubscribe <http://mail.python.org/mailman/options/python-list>, <mailto:python-list-request@python.org?subject=unsubscribe>
List-Archive <http://mail.python.org/pipermail/python-list/>
List-Post <mailto:python-list@python.org>
List-Help <mailto:python-list-request@python.org?subject=help>
List-Subscribe <http://mail.python.org/mailman/listinfo/python-list>, <mailto:python-list-request@python.org?subject=subscribe>
Newsgroups comp.lang.python
Message-ID <mailman.3254.1363135456.2939.python-list@python.org> (permalink)
Lines 57
NNTP-Posting-Host 2001:888:2000:d::a6
X-Trace 1363135456 news.xs4all.nl 6950 [2001:888:2000:d::a6]:60276
X-Complaints-To abuse@xs4all.nl
Xref csiph.com comp.lang.python:41157

Show key headers only | View raw


On 13 March 2013 00:21, Jiewei Huang <jiewei24@gmail.com> wrote:
> 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.

Perhaps an example will help. Let's say we have a variable called x
that we initialise with

x = 2

Here's a line of code that prints 2*x:

print(2 * x)

This will print out 4 but that's not what you want. Here's a function
that prints its argument multiplied by 2:

def double(y):
    print(2 * y)

Now we have a function and we can call it with

double(x)

so that it prints 4. Again, though, you didn't want to print it. You
wanted to *return* the value. So here's a function that *returns* 2
times its argument:

def double(x):
    return 2 * x

Now if we do

z = double(x)

z will have the value 4. You can check this with

print(z)

Try the code above and see if you can apply the same principles to your problem.


Oscar

Back to comp.lang.python | Previous | NextPrevious in thread | Next in thread | Find similar | Unroll thread


Thread

A string and an integer to appear in tuple (python 2.7) Jiewei Huang <jiewei24@gmail.com> - 2013-03-12 17:21 -0700
  Re: A string and an integer to appear in tuple (python 2.7) Oscar Benjamin <oscar.j.benjamin@gmail.com> - 2013-03-13 00:43 +0000
  Re: A string and an integer to appear in tuple (python 2.7) Vlastimil Brom <vlastimil.brom@gmail.com> - 2013-03-13 02:06 +0100

csiph-web