Confessions of a Python fanboy

MRAB python at mrabarnett.plus.com
Thu Jul 30 14:20:11 EDT 2009


superpollo wrote:
> r wrote:
>> On Jul 30, 12:15 pm, Masklinn <maskl... at masklinn.net> wrote:
>> [snip]
>>
>>> Furthermore Ruby has a pretty nice convention (sadly not used enough 
>>> I  think) taken from Scheme where it's possible to postfix a method 
>>> name  with "!" (note: the "!" is part of the name, there's no magic) 
>>> to  indicate that this method modifies the object it's called on 
>>> rather  than simply returning stuff.
>>
>>
>> Another oddity i did not like at first but must admit is growing on me
>>   vector.reverse --> returns a new reversed vector
>>   vector.reverse! --> modifies the instance vector in-place
>>
>> Of course in python you would do...
>>   vector.reverse --> in-place
>>   vector.reversed --> in-place
>>
> 
> how to reverse a string in python? must i usa a temp? like:
> 
>  >>> s = "ciccio"
>  >>> l = list(s)
>  >>> l.reverse()
>  >>> s = "".join(l)
>  >>> s
> 'oiccic'
>  >>>
> 
> ???
> 
Use slicing with a step of -1:

 >>> s = "ciccio"
 >>> s[::-1]
'oiccic'




More information about the Python-list mailing list