[Tutor] string codes

Alan Gauld alan.gauld at btinternet.com
Tue Nov 26 11:01:14 CET 2013


On 26/11/13 09:19, 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)

If I understand what you want then I think its the ord() function
you are looking for

c = 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

I assume you mean  test = s[1:-1] == "bcd"

>      test = s.sub_is(1, -1, "bcd")    # yes! what I'm searching

You can use startswith() (or endswith) by providing the optional
start and end arguments:

test = s.startswith("bcd", 1, -1)

The help() command at the >>> prompt is your friend.
Try

 >>> help(str)

-- 
Alan G
Author of the Learn to Program web site
http://www.alan-g.me.uk/
http://www.flickr.com/photos/alangauldphotos



More information about the Tutor mailing list