Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!newsfeed.xs4all.nl!newsfeed1.news.xs4all.nl!xs4all!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.015 X-Spam-Evidence: '*H*': 0.97; '*S*': 0.00; 'string': 0.09; 'specified,': 0.09; 'underscore': 0.09; 'subject:question': 0.10; 'delimiter': 0.16; 'from:addr:rosuav': 0.16; 'from:name:chris angelico': 0.16; 'given,': 0.16; 'separator.': 0.16; 'splits': 0.16; 'wrote:': 0.18; 'split': 0.19; '>>>': 0.22; 'string,': 0.24; 'appreciated': 0.26; 'skip:" 40': 0.26; 'header:In-Reply-To:1': 0.27; 'tried': 0.27; 'function': 0.29; 'am,': 0.29; 'words': 0.29; 'message-id:@mail.gmail.com': 0.30; 'work.': 0.31; "skip:' 10": 0.31; 'sep': 0.31; 'fri,': 0.33; 'done.': 0.35; 'but': 0.35; 'received:google.com': 0.35; 'should': 0.36; 'list': 0.37; 'easily': 0.37; 'starting': 0.37; 'skip:[ 10': 0.38; 'to:addr :python-list': 0.38; 'to:addr:python.org': 0.39; 'most': 0.60; '1:00': 0.84; 'front.': 0.84; '2013': 0.98 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=YtYZvm48fvTp6M6X2Hh054qpnea2OS/3Ut7VYsWIGT8=; b=UPBKX10ZKtozioufh/SLFK0NUK2vJSLY6PlYJqnzobzeHAP1sBKa4nlW2U1nl0t9i/ pukYXPTL6tdgmI0zqxtY5AmDJzDeMKknz7+7IgKy3s7oTsZSQ4VOxPvvCJSDKiWbVc8T sc+qvG+2PlmqZIM/q4ma//Nfdlsq+G/jWgzK1ScW2PHAFhZFtmQnTqKRqnibU67Tjlkz 4V6Kq6lqfd8jRi8Cprh+oqnyMfbbnTBZ+U8gcMGQgADyWBWsMnPCcuKd8xSk5JRsI9tw YIaDlrJoczf8FM089VBnd3CcpKeiQ0Zu7AKzFa8Mffelb/RLTO7atKCSGxoQE6kog9ba joVg== MIME-Version: 1.0 X-Received: by 10.220.167.69 with SMTP id p5mr26560974vcy.57.1368717339331; Thu, 16 May 2013 08:15:39 -0700 (PDT) In-Reply-To: References: Date: Fri, 17 May 2013 01:15:39 +1000 Subject: Re: spilt question From: Chris Angelico 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: 32 NNTP-Posting-Host: 2001:888:2000:d::a6 X-Trace: 1368717347 news.xs4all.nl 15966 [2001:888:2000:d::a6]:35764 X-Complaints-To: abuse@xs4all.nl Xref: csiph.com comp.lang.python:45432 On Fri, May 17, 2013 at 1:00 AM, loial wrote: > I want to split a string so that I always return everything BEFORE the LAST underscore > > HELLO_xxxxxxxx.lst # should return HELLO > HELLO_GOODBYE_xxxxxxxx.ls # should return HELLO_GOODBYE > > I have tried with rsplit but cannot get it to work. > > Any help appreciated Try with a limit: >>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1) ['HELLO_GOODBYE', 'xxxxxxxx.ls'] >>> "HELLO_GOODBYE_xxxxxxxx.ls".rsplit("_",1)[0] 'HELLO_GOODBYE' You can easily get docs on it: >>> help("".rsplit) Help on built-in function rsplit: rsplit(...) S.rsplit(sep=None, maxsplit=-1) -> list of strings Return a list of the words in S, using sep as the delimiter string, starting at the end of the string and working to the front. If maxsplit is given, at most maxsplit splits are done. If sep is not specified, any whitespace string is a separator. ChrisA