Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: MRAB Newsgroups: comp.lang.python Subject: Re: 'string.join' is wrong in my Python console Date: Thu, 3 Dec 2015 16:24:07 +0000 Lines: 65 Message-ID: References: <88afafc5-699f-46ea-aaca-7e78b75a4552@googlegroups.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de Cf770N/zCtJCLjmkPa+dsgHtYdkMNY0RSG4Dphx/POJg== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.000 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'subject:Python': 0.05; 'classes,': 0.05; 'deprecated': 0.07; 'line:': 0.07; "subject:' ": 0.07; 'nameerror:': 0.09; 'tuple': 0.09; 'python': 0.10; '2.7': 0.13; 'importing': 0.15; 'instead.': 0.15; '*i*': 0.16; 'constants': 0.16; 'from:addr:mrabarnett.plus.com': 0.16; 'from:addr:python': 0.16; 'from:name:mrab': 0.16; 'guess.': 0.16; 'last)': 0.16; 'message-id:@mrabarnett.plus.com': 0.16; 'nameerror': 0.16; 'received:192.168.1.4': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'url:faq': 0.16; 'wrote:': 0.16; 'string': 0.17; 'module,': 0.18; "shouldn't": 0.18; 'try:': 0.18; '>>>': 0.20; 'arguments': 0.22; 'trying': 0.22; 'defined': 0.23; 'import': 0.24; '(most': 0.24; 'header:In-Reply-To:1': 0.24; 'module': 0.25; 'header:User-Agent:1': 0.26; 'correct': 0.28; 'function': 0.28; 'context,': 0.29; 'tutorial': 0.29; 'allows': 0.30; 'too.': 0.30; "i'd": 0.31; 'error.': 0.31; 'useful': 0.33; 'problem': 0.33; 'url:python': 0.33; 'legacy': 0.33; 'traceback': 0.33; 'skip:- 10': 0.34; 'running': 0.34; 'list': 0.34; 'could': 0.35; 'replaced': 0.35; 'robert': 0.35; 'something': 0.35; 'should': 0.36; 'instead': 0.36; 'url:org': 0.36; 'to:addr:python- list': 0.36; 'pm,': 0.36; 'method': 0.37; 'hi,': 0.38; 'why': 0.39; 'skip:- 20': 0.39; 'received:192': 0.39; 'to:addr:python.org': 0.40; 'some': 0.40; 'else.': 0.66; 'link:': 0.69; "'2',": 0.84; '2.7.': 0.84; 'herron:': 0.84 X-CM-Score: 0.00 X-CNFS-Analysis: v=2.1 cv=CvRCCSMD c=1 sm=1 tr=0 a=0nF1XD0wxitMEM03M9B4ZQ==:117 a=0nF1XD0wxitMEM03M9B4ZQ==:17 a=0Bzu9jTXAAAA:8 a=EBOSESyhAAAA:8 a=IkcTkHD0fZMA:10 a=8AHkEIZyAAAA:8 a=qxIOjgwTfSQRBWRqMqkA:9 a=QEXdDO2ut3YA:10 a=fHXwG-QLKeAA:10 X-AUTH: mrabarnett@:2500 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:38.0) Gecko/20100101 Thunderbird/38.3.0 In-Reply-To: X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:99969 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) >>> in () >>> ----> 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.