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


Groups > comp.lang.python > #99936 > unrolled thread

'string.join' is wrong in my Python console

Started byRobert <rxjwg98@gmail.com>
First post2015-12-02 22:55 -0800
Last post2015-12-03 19:16 +0100
Articles 12 — 9 participants

Back to article view | Back to comp.lang.python


Contents

  'string.join' is wrong in my Python console Robert <rxjwg98@gmail.com> - 2015-12-02 22:55 -0800
    Re: 'string.join' is wrong in my Python console dieter <dieter@handshake.de> - 2015-12-03 08:39 +0100
    Re: 'string.join' is wrong in my Python console Sijan Bhandari <sijanonly@gmail.com> - 2015-12-02 23:59 -0800
    Re: 'string.join' is wrong in my Python console Gary Herron <gherron@digipen.edu> - 2015-12-03 01:02 -0800
      Re: 'string.join' is wrong in my Python console Robin Koch <robin.koch@t-online.de> - 2015-12-03 17:00 +0100
        Re: 'string.join' is wrong in my Python console MRAB <python@mrabarnett.plus.com> - 2015-12-03 16:24 +0000
        Re: 'string.join' is wrong in my Python console Ian Kelly <ian.g.kelly@gmail.com> - 2015-12-03 09:25 -0700
          Re: 'string.join' is wrong in my Python console Robin Koch <robin.koch@t-online.de> - 2015-12-03 18:01 +0100
            Re: 'string.join' is wrong in my Python console Mark Lawrence <breamoreboy@yahoo.co.uk> - 2015-12-03 17:42 +0000
              Re: 'string.join' is wrong in my Python console Robin Koch <robin.koch@t-online.de> - 2015-12-03 19:15 +0100
        Re: 'string.join' is wrong in my Python console Terry Reedy <tjreedy@udel.edu> - 2015-12-03 12:23 -0500
          Re: 'string.join' is wrong in my Python console Robin Koch <robin.koch@t-online.de> - 2015-12-03 19:16 +0100

#99936 — 'string.join' is wrong in my Python console

FromRobert <rxjwg98@gmail.com>
Date2015-12-02 22:55 -0800
Subject'string.join' is wrong in my Python console
Message-ID<88afafc5-699f-46ea-aaca-7e78b75a4552@googlegroups.com>
Hi,

I read the tutorial on "Why is join() a string method instead of a list
 or tuple method?"
at link:
https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls

I have a problem on running the last line:
---------------
If none of these arguments persuade you, then for the moment you can
 continue to use the join() function from the string module, which allows
 you to write

string.join(['1', '2', '4', '8', '16'], ", ")
-----------------------

My Python console is 2.7. It should be no problem because I see the tutorial
is 2.7 too.

The console has these display:

string.join(['1', '2', '4', '8', '16'], ", ")
---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-15-3947890a7e6e> in <module>()
----> 1 string.join(['1', '2', '4', '8', '16'], ", ")

NameError: name 'string' is not defined 


From the context, I don't see string should be replaced by something else.

Could you tell me why I have such an error?


Thanks,

[toc] | [next] | [standalone]


#99938

Fromdieter <dieter@handshake.de>
Date2015-12-03 08:39 +0100
Message-ID<mailman.160.1449128446.14615.python-list@python.org>
In reply to#99936
Robert <rxjwg98@gmail.com> writes:

> I read the tutorial on "Why is join() a string method instead of a list
>  or tuple method?"
> at link:
> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
>
> I have a problem on running the last line:
> ---------------
> If none of these arguments persuade you, then for the moment you can
>  continue to use the join() function from the string module, which allows
>  you to write
>
> string.join(['1', '2', '4', '8', '16'], ", ")
> -----------------------
>
> My Python console is 2.7. It should be no problem because I see the tutorial
> is 2.7 too.
>
> The console has these display:
>
> string.join(['1', '2', '4', '8', '16'], ", ")
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
> <ipython-input-15-3947890a7e6e> in <module>()
> ----> 1 string.join(['1', '2', '4', '8', '16'], ", ")
>
> NameError: name 'string' is not defined 

You need to import the "string" module before you can use it.

[toc] | [prev] | [next] | [standalone]


#99939

FromSijan Bhandari <sijanonly@gmail.com>
Date2015-12-02 23:59 -0800
Message-ID<5be09585-395d-499c-9cd6-a1e5f5459aa6@googlegroups.com>
In reply to#99936
On Thursday, December 3, 2015 at 12:40:52 PM UTC+5:45, Robert wrote:
> Hi,
> 
> I read the tutorial on "Why is join() a string method instead of a list
>  or tuple method?"
> at link:
> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
> 
> I have a problem on running the last line:
> ---------------
> If none of these arguments persuade you, then for the moment you can
>  continue to use the join() function from the string module, which allows
>  you to write
> 
> string.join(['1', '2', '4', '8', '16'], ", ")
> -----------------------
> 
> My Python console is 2.7. It should be no problem because I see the tutorial
> is 2.7 too.
> 
> The console has these display:
> 
> string.join(['1', '2', '4', '8', '16'], ", ")
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
> <ipython-input-15-3947890a7e6e> in <module>()
> ----> 1 string.join(['1', '2', '4', '8', '16'], ", ")
> 
> NameError: name 'string' is not defined 
> 
> 
> From the context, I don't see string should be replaced by something else.
> 
> Could you tell me why I have such an error?
> 
> 
> Thanks,

simply, import string at the beginning of your script.

[toc] | [prev] | [next] | [standalone]


#99942

FromGary Herron <gherron@digipen.edu>
Date2015-12-03 01:02 -0800
Message-ID<mailman.162.1449133357.14615.python-list@python.org>
In reply to#99936
On 12/02/2015 10:55 PM, Robert wrote:
> Hi,
>
> I read the tutorial on "Why is join() a string method instead of a list
>   or tuple method?"
> at link:
> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
>
> I have a problem on running the last line:
> ---------------
> If none of these arguments persuade you, then for the moment you can
>   continue to use the join() function from the string module, which allows
>   you to write
>
> string.join(['1', '2', '4', '8', '16'], ", ")
> -----------------------
>
> My Python console is 2.7. It should be no problem because I see the tutorial
> is 2.7 too.
>
> The console has these display:
>
> string.join(['1', '2', '4', '8', '16'], ", ")
> ---------------------------------------------------------------------------
> NameError                                 Traceback (most recent call last)
> <ipython-input-15-3947890a7e6e> in <module>()
> ----> 1 string.join(['1', '2', '4', '8', '16'], ", ")
>
> NameError: name 'string' is not defined
>
>
>  From the context, I don't see string should be replaced by something else.
>
> Could you tell me why I have such an error?

You are trying to use the *string* module without importing it, I'd guess.

Try:
     import string
first then you should be able to access string.join without error.

Gary Herron




>
>
> Thanks,


-- 
Dr. Gary Herron
Department of Computer Science
DigiPen Institute of Technology
(425) 895-4418

[toc] | [prev] | [next] | [standalone]


#99963

FromRobin Koch <robin.koch@t-online.de>
Date2015-12-03 17:00 +0100
Message-ID<n3pov1$ih2$1@news.albasani.net>
In reply to#99942
Am 03.12.2015 um 10:02 schrieb Gary Herron:
> On 12/02/2015 10:55 PM, Robert wrote:
>> Hi,
>>
>> I read the tutorial on "Why is join() a string method instead of a list
>>   or tuple method?"
>> at link:
>> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
>>
>>
>> I have a problem on running the last line:
>> ---------------
>> If none of these arguments persuade you, then for the moment you can
>>   continue to use the join() function from the string module, which
>> allows
>>   you to write
>>
>> string.join(['1', '2', '4', '8', '16'], ", ")
>> -----------------------
>>
>> My Python console is 2.7. It should be no problem because I see the
>> tutorial
>> is 2.7 too.
>>
>> The console has these display:
>>
>> string.join(['1', '2', '4', '8', '16'], ", ")
>> ---------------------------------------------------------------------------
>>
>> NameError                                 Traceback (most recent call
>> last)
>> <ipython-input-15-3947890a7e6e> in <module>()
>> ----> 1 string.join(['1', '2', '4', '8', '16'], ", ")
>>
>> NameError: name 'string' is not defined
>>
>>
>>  From the context, I don't see string should be replaced by something
>> else.
>>
>> Could you tell me why I have such an error?
>
> You are trying to use the *string* module without importing it, I'd guess.
>
> Try:
>      import string
> first then you should be able to access string.join without error.

Now *I* am confused.

Shouldn't it be

", ".join(['1', '2', '4', '8', '16'])

instead? Without any importing?

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#99969

FromMRAB <python@mrabarnett.plus.com>
Date2015-12-03 16:24 +0000
Message-ID<mailman.177.1449159850.14615.python-list@python.org>
In reply to#99963
On 2015-12-03 16:00, Robin Koch wrote:
> Am 03.12.2015 um 10:02 schrieb Gary Herron:
>> On 12/02/2015 10:55 PM, Robert wrote:
>>> Hi,
>>>
>>> I read the tutorial on "Why is join() a string method instead of a list
>>>   or tuple method?"
>>> at link:
>>> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
>>>
>>>
>>> I have a problem on running the last line:
>>> ---------------
>>> If none of these arguments persuade you, then for the moment you can
>>>   continue to use the join() function from the string module, which
>>> allows
>>>   you to write
>>>
>>> string.join(['1', '2', '4', '8', '16'], ", ")
>>> -----------------------
>>>
>>> My Python console is 2.7. It should be no problem because I see the
>>> tutorial
>>> is 2.7 too.
>>>
>>> The console has these display:
>>>
>>> string.join(['1', '2', '4', '8', '16'], ", ")
>>> ---------------------------------------------------------------------------
>>>
>>> NameError                                 Traceback (most recent call
>>> last)
>>> <ipython-input-15-3947890a7e6e> in <module>()
>>> ----> 1 string.join(['1', '2', '4', '8', '16'], ", ")
>>>
>>> NameError: name 'string' is not defined
>>>
>>>
>>>  From the context, I don't see string should be replaced by something
>>> else.
>>>
>>> Could you tell me why I have such an error?
>>
>> You are trying to use the *string* module without importing it, I'd guess.
>>
>> Try:
>>      import string
>> first then you should be able to access string.join without error.
>
> Now *I* am confused.
>
> Shouldn't it be
>
> ", ".join(['1', '2', '4', '8', '16'])
>
> instead? Without any importing?
>
The documentation says: """The string module contains a number of
useful constants and classes, as well as some deprecated legacy
functions that are also available as methods on strings."""

The "join" function is one of those old functions you don't need any
more, and you're correct that the the "join" method should be used
instead.

[toc] | [prev] | [next] | [standalone]


#99970

FromIan Kelly <ian.g.kelly@gmail.com>
Date2015-12-03 09:25 -0700
Message-ID<mailman.178.1449159988.14615.python-list@python.org>
In reply to#99963
On Thu, Dec 3, 2015 at 9:00 AM, Robin Koch <robin.koch@t-online.de> wrote:
> Now *I* am confused.
>
> Shouldn't it be
>
> ", ".join(['1', '2', '4', '8', '16'])
>
> instead? Without any importing?

That would be the normal way to write it. The FAQ entry is suggesting
the string module function as an alternative for those who can't
accept it as a string method.

[toc] | [prev] | [next] | [standalone]


#99974

FromRobin Koch <robin.koch@t-online.de>
Date2015-12-03 18:01 +0100
Message-ID<n3psh8$pnu$1@news.albasani.net>
In reply to#99970
Am 03.12.2015 um 17:25 schrieb Ian Kelly:
> On Thu, Dec 3, 2015 at 9:00 AM, Robin Koch <robin.koch@t-online.de> wrote:
>> Now *I* am confused.
>>
>> Shouldn't it be
>>
>> ", ".join(['1', '2', '4', '8', '16'])
>>
>> instead? Without any importing?
>
> That would be the normal way to write it. The FAQ entry is suggesting
> the string module function as an alternative for those who can't
> accept it as a string method.

Whoops.
Thanks and sorry. :-)

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#99978

FromMark Lawrence <breamoreboy@yahoo.co.uk>
Date2015-12-03 17:42 +0000
Message-ID<mailman.183.1449164570.14615.python-list@python.org>
In reply to#99974
On 03/12/2015 17:01, Robin Koch wrote:
> Am 03.12.2015 um 17:25 schrieb Ian Kelly:
>> On Thu, Dec 3, 2015 at 9:00 AM, Robin Koch <robin.koch@t-online.de>
>> wrote:
>>> Now *I* am confused.
>>>
>>> Shouldn't it be
>>>
>>> ", ".join(['1', '2', '4', '8', '16'])
>>>
>>> instead? Without any importing?
>>
>> That would be the normal way to write it. The FAQ entry is suggesting
>> the string module function as an alternative for those who can't
>> accept it as a string method.
>
> Whoops.
> Thanks and sorry. :-)
>

You might like to note that here 
https://docs.python.org/3/faq/design.html#why-is-join-a-string-method-instead-of-a-list-or-tuple-method 
the reference to the string module function has been completely removed.

-- 
My fellow Pythonistas, ask not what our language can do for you, ask
what you can do for our language.

Mark Lawrence

[toc] | [prev] | [next] | [standalone]


#99979

FromRobin Koch <robin.koch@t-online.de>
Date2015-12-03 19:15 +0100
Message-ID<n3q0r7$2pj$1@news.albasani.net>
In reply to#99978
Am 03.12.2015 um 18:42 schrieb Mark Lawrence:
> On 03/12/2015 17:01, Robin Koch wrote:
>> Am 03.12.2015 um 17:25 schrieb Ian Kelly:
>>> On Thu, Dec 3, 2015 at 9:00 AM, Robin Koch <robin.koch@t-online.de>
>>> wrote:
>>>> Now *I* am confused.
>>>>
>>>> Shouldn't it be
>>>>
>>>> ", ".join(['1', '2', '4', '8', '16'])
>>>>
>>>> instead? Without any importing?
>>>
>>> That would be the normal way to write it. The FAQ entry is suggesting
>>> the string module function as an alternative for those who can't
>>> accept it as a string method.
>>
>> Whoops.
>> Thanks and sorry. :-)
>>
>
> You might like to note that here
> https://docs.python.org/3/faq/design.html#why-is-join-a-string-method-instead-of-a-list-or-tuple-method
> the reference to the string module function has been completely removed.

Actually I did. :-)
I tried the line with my Python 3.4 and it didn't work.
Then I sent my initial posting.

After Ians answer I took a look in the 2.x and the 3.x FAQs and found 
exactly that phrase removed. :-)

-- 
Robin Koch

[toc] | [prev] | [next] | [standalone]


#99976

FromTerry Reedy <tjreedy@udel.edu>
Date2015-12-03 12:23 -0500
Message-ID<mailman.181.1449163450.14615.python-list@python.org>
In reply to#99963
On 12/3/2015 11:00 AM, Robin Koch wrote:
> Am 03.12.2015 um 10:02 schrieb Gary Herron:
>> On 12/02/2015 10:55 PM, Robert wrote:
>>> Hi,
>>>
>>> I read the tutorial on "Why is join() a string method instead of a list
>>>   or tuple method?"
>>> at link:
>>> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
>>>
>>>
>>>
>>> I have a problem on running the last line:
>>> ---------------
>>> If none of these arguments persuade you, then for the moment you can
>>>   continue to use the join() function from the string module, which
>>> allows
>>>   you to write
>>>
>>> string.join(['1', '2', '4', '8', '16'], ", ")
>>> -----------------------
...
>> You are trying to use the *string* module without importing it, I'd
>> guess.
>>
>> Try:
>>      import string
>> first then you should be able to access string.join without error.
>
> Now *I* am confused.
>
> Shouldn't it be
>
> ", ".join(['1', '2', '4', '8', '16'])
>
> instead? Without any importing?

Yes, that is what one *should* do in late 2.x and indeed must do in 3.x, 
where the string module has been stripped of the functions that later 
became string methods.  The FAQ entry was written when the join method 
was new as a method and some people were upset by the reversal of the 
order of the two arguments, an iterable of strings and the joining string.

-- 
Terry Jan Reedy

[toc] | [prev] | [next] | [standalone]


#99980

FromRobin Koch <robin.koch@t-online.de>
Date2015-12-03 19:16 +0100
Message-ID<n3q0tm$2pj$2@news.albasani.net>
In reply to#99976
Am 03.12.2015 um 18:23 schrieb Terry Reedy:
> On 12/3/2015 11:00 AM, Robin Koch wrote:
>> Am 03.12.2015 um 10:02 schrieb Gary Herron:
>>> On 12/02/2015 10:55 PM, Robert wrote:
>>>> Hi,
>>>>
>>>> I read the tutorial on "Why is join() a string method instead of a list
>>>>   or tuple method?"
>>>> at link:
>>>> https://docs.python.org/2/faq/design.html#why-must-self-be-used-explicitly-in-method-definitions-and-calls
>>>>
>>>>
>>>>
>>>>
>>>> I have a problem on running the last line:
>>>> ---------------
>>>> If none of these arguments persuade you, then for the moment you can
>>>>   continue to use the join() function from the string module, which
>>>> allows
>>>>   you to write
>>>>
>>>> string.join(['1', '2', '4', '8', '16'], ", ")
>>>> -----------------------
> ...
>>> You are trying to use the *string* module without importing it, I'd
>>> guess.
>>>
>>> Try:
>>>      import string
>>> first then you should be able to access string.join without error.
>>
>> Now *I* am confused.
>>
>> Shouldn't it be
>>
>> ", ".join(['1', '2', '4', '8', '16'])
>>
>> instead? Without any importing?
>
> Yes, that is what one *should* do in late 2.x and indeed must do in 3.x,
> where the string module has been stripped of the functions that later
> became string methods.  The FAQ entry was written when the join method
> was new as a method and some people were upset by the reversal of the
> order of the two arguments, an iterable of strings and the joining string.

Thank you.
I figured that out by now. :-)

I just didn't followed the link of the OP.

-- 
Robin Koch

[toc] | [prev] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web