Thread: InStr()

    #1
  1. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154

    InStr()


    I was wondering if Python had a feature in it that functions like InStr() does in Visual Basic. If it does, can someone please show me?
  2. #2
  3. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    InStr() returns an integer position of the first occurence of the pattern in a string, right?
    You need string's index() function:
    Code:
       >>> import string
       >>> teststring = "abcdefgh"
       >>> i = string.index(teststring, 'e')
       >>> print i
       4
       >>>
    Am I supposed to sign here?
  4. #3
  5. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Not a function per-say but with Python You'd use the in keyword

    Code:
    >>> aString = 'some simple string'
    >>> 'some' in aString
    True
    >>> 'som' in aString
    True
    >>> 'simple' in aString
    True
    >>> 'bla' in aString
    False
    >>> 'none' in aString
    False
    >>>
    Mark.
    programming language development: www.netytan.com Hula

  6. #4
  7. A wanna-be guru of some sort
    Devshed Novice (500 - 999 posts)

    Join Date
    Sep 2004
    Location
    Either online or offline
    Posts
    625
    Rep Power
    20
    InStr():
    Visual Basic Language Reference
    InStr Function



    Returns an integer specifying the start position of the first occurrence of one string within another.
    I thought he wanted the idex where it appears... If it doesn't appear, index() returns -1.

    Is that what you meant, †Yegg†?
    Am I supposed to sign here?
  8. #5
  9. Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Dec 2004
    Location
    Meriden, Connecticut
    Posts
    1,797
    Rep Power
    154
    Wow, thanks so much netytan. It works great. I've been trying to convert some code from a bot I made in Visual Basic so I may need help with like a few more things. This includes the Visual Basic command LCase() and Chr(). How I'd use those in Python.
    Edit: I din't noticed you posted again, we posted like 1 minute difference. What I wanted was something that could find if a certain string was inside of another string. The in function worked fine.
  10. #6
  11. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    Having not used VB before I'm not familiar with what these functions do exactly; specifically Chr() but you should be able to find the string methods you're looking for by doing:

    Code:
    >>> help(str)
    Help on class str in module __builtin__:
    
    class str(basestring)
     |  str(object) -> string
     |  
     |  Return a nice string representation of the object.
     |  If the argument is a string, the return value is the same object.
     |  
     |  Method resolution order:
     |      str
     |      basestring
     |      object
     |  
     |  Methods defined here:
    ...
     |  
     |  lower(...)
     |      S.lower() -> string
     |      
     |      Return a copy of the string S converted to lowercase.
     |
    ...
    >>>
    Also, you might like to check out the string module: http://www.python.org/doc/2.4/lib/module-string.html

    Mark.
    programming language development: www.netytan.com Hula

  12. #7
  13. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,638
    Rep Power
    4247
    chr() is built into python.
    Code:
    >>> print chr(65)
    A
    Up the Irons
    What Would Jimi Do? Smash amps. Burn guitar. Take the groupies home.
    "Death Before Dishonour, my Friends!!" - Bruce D ickinson, Iron Maiden Aug 20, 2005 @ OzzFest
    Down with Sharon Osbourne

    "I wouldn't hire a butcher to fix my car. I also wouldn't hire a marketing firm to build my website." - Nilpo

IMN logo majestic logo threadwatch logo seochat tools logo