[Zope] mid function

Eric Walstad eric@walstads.net
Sat, 11 Aug 2001 13:26:26 -0700


Hello again, Hamzat.
Like VB, Python has great string support.
As far as I know, there is no direct equivalent function in Python for VB's
mid() function.  However, because you know the location of the sub string
(the start and length), it's just as easy with Python as with VB's mid()
function.  Python treats strings a list of characters, so you can reference
just those characters you are interested in by using the "slicing operator":

var_name[start_pos:end_pos]

strTitle = "sunday05082001"
strDay = strTitle[0:6]  # strTitle[:6] will do the same thing
print strDay => "sunday"

Note that if you used [1:6] you'd end up with "unday".  I like to think of
the index numbers as representing the border of the letter, not the letter
itself; in strTitle, 'y' is after border 5 and before border 6.

In dtml it would look something like this:
<dtml-var expr="strTitle[0:6]">

You should have a look at the documentation for the string module to see how
other things are done in Python.  Like I said before, it's very flexible and
powerful! (www.python.org)

Hope that helps,
Eric.

> -----Original Message-----
> From: zope-admin@zope.org [mailto:zope-admin@zope.org]On Behalf Of
> Hamzat kamal
> Sent: Saturday, August 11, 2001 10:02 AM
> To: zope@zope.org
> Subject: [Zope] mid function
>
>
>
>
> I checked through the DTML function i could not find this function.
> I am writing a code where i will need to store the title of each dtml
> document with <dtml-let > and extract certain number of xters from the
> title and test it before calling a particular object.
>
> This function is widely used, but this is the way is been used in Vb
>
> mid(string, start [,length])
>   string - denote the var name
>   start  -   "    where you want to begin the extraction
>   length -   "    the no of xters you want extract from the string
>
> E.g.
>
> let say my title is sunday05082001
> then if i specify mid(title, 1, 6)
> the result of the fuction will be sunday.
>
> kindly help me out.
>
>
>
> _______________________________________________
> Zope maillist  -  Zope@zope.org
> http://lists.zope.org/mailman/listinfo/zope
> **   No cross posts or HTML encoding!  **
> (Related lists -
>  http://lists.zope.org/mailman/listinfo/zope-announce
>  http://lists.zope.org/mailman/listinfo/zope-dev )