Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.python > #41156 > unrolled thread
| Started by | Jiewei Huang <jiewei24@gmail.com> |
|---|---|
| First post | 2013-03-12 17:21 -0700 |
| Last post | 2013-03-13 02:06 +0100 |
| Articles | 3 — 3 participants |
Back to article view | Back to comp.lang.python
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
| From | Jiewei Huang <jiewei24@gmail.com> |
|---|---|
| Date | 2013-03-12 17:21 -0700 |
| Subject | A string and an integer to appear in tuple (python 2.7) |
| Message-ID | <36706e17-0a2d-46eb-b222-09b90ec6ab58@googlegroups.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.
[toc] | [next] | [standalone]
| From | Oscar Benjamin <oscar.j.benjamin@gmail.com> |
|---|---|
| Date | 2013-03-13 00:43 +0000 |
| Message-ID | <mailman.3254.1363135456.2939.python-list@python.org> |
| In reply to | #41156 |
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
[toc] | [prev] | [next] | [standalone]
| From | Vlastimil Brom <vlastimil.brom@gmail.com> |
|---|---|
| Date | 2013-03-13 02:06 +0100 |
| Message-ID | <mailman.3255.1363136823.2939.python-list@python.org> |
| In reply to | #41156 |
2013/3/13 Jiewei Huang <jiewei24@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
[toc] | [prev] | [standalone]
Back to top | Article view | comp.lang.python
csiph-web