On Tue, Nov 26, 2013 at 10:19:46AM +0100, spir wrote:

> What is the method to get a code or list of codes inside a string:
>       s = "abcde"
>       c = s.code(2)
>       assert(c == 0x63)
> ?

Use indexing to get the character you want, then ord() to return its 
ordinal value.

ord(s[2])

> === sub compare ===
> 
> Is there a method to compare a substring, without building a substring from 
> the big one? Like startswith or endswith, but anywhere inside the string?
>       test = s[1, -1] == "bcd"        # no!, builds a substring
>       test = s.sub_is(1, -1, "bcd")   # yes! what I'm searching

The word I think you want is "view". A view is a look inside another 
object without copying the part that you want.

I think that views would be useful for *very large strings*, but very 
large probably means a lot larger than you might think. For small 
strings, say under a few hundred or perhaps even thousand characters, 
making a copy of the substring will probably be faster.

I say "probably", but I'm only guessing, because strings in Python don't 
have views. (Perhaps they should?)

So for the time being, don't worry about it. Copying a substring in 
Python is very efficient, the simplest way is to just compare the 
substring directly:

s[1:-1] = "bcd"

Take note that a slice (substring) of a string uses a colon : and not a 
comma.


-- 
Steven
_______________________________________________
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor

Reply via email to