Re: string methods
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.python archive

Re: string methods

From: Peter Hansen <peter@engcorp.com>
Date: Sat Jul 30 2005 - 19:48:17 CEST

Brian Beck wrote:
> anthonyberet wrote:
>>I know this touches on immutability etc, but I can't find string methods
>>to return the first 3 characters, and then the last 2 characters, which
>>I could concatenate with newchar to make a new string.
>
> As tiissa said, you want slicing:
>
> py> s = "foobar"
> py> s[:3]
> 'foo'
> py> s[:3] + "B" + s[4:]
> 'fooBar'

And if that's too ugly for you and you think you need to do this
operation a lot, just define a function to do it for you based on the
index value and string that you pass in to it.

-Peter
Received on Thu Sep 29 17:16:10 2005