|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
||||
|
||||
|
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
|
||||
|
||||
|
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?
|
|
#3
|
||||
|
||||
|
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. |
|
#4
|
||||
|
||||
|
InStr():
Quote:
Is that what you meant, †Yegg†? |
|
#5
|
||||
|
||||
|
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. |
|
#6
|
||||
|
||||
|
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. |
|
#7
|
||||
|
||||
|
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 Puzzle of the Month solved by Keath and KevinADC, superior perl programmers of the month |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Python Programming > InStr() |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|