[Tutor] OOPs Concept

Matthew White mwhite3 at ttsd.k12.or.us
Wed Apr 19 17:49:18 CEST 2006


On Wed, Apr 19, 2006 at 09:10:41PM +0530, Kaushal Shriyan (kaushalshriyan at gmail.com) wrote:
> Thanks Matthew
> Just wanted to know
> x.count('l') -> 2 Here 2 means what I didnot understood this
> and also does x is a object and capitalize(), swapcase()  and
> count('l') are methods, is that correct what i understand

yes, count(), capitalize(), and swapcase() are called methods.

The count() method counts the number of the given string in the object.

x = 'hello'
x.count('l')     -> 2
x.count('h')     -> 1

y = 'good morning'
y.count('o')     -> 3
y.count('oo')    -> 1

you can find out which methods belong to an object using the dir()
function and can get help for a method by using the help() function:

% python
Python 2.3.5 (#2, Sep  4 2005, 22:01:42) 
[GCC 3.3.5 (Debian 1:3.3.5-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> x = 'hello'
>>> dir(x)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__',
'__eq__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__',
'__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__',
'__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__',
'__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__',
'__str__', 'capitalize', 'center', 'count', 'decode', 'encode',
'endswith', 'expandtabs', 'find', 'index', 'isalnum', 'isalpha',
'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join', 'ljust',
'lower', 'lstrip', 'replace', 'rfind', 'rindex', 'rjust', 'rstrip',
'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title',
'translate', 'upper', 'zfill']
>>> help(x.swapcase)
Help on built-in function swapcase:

swapcase(...)
	S.swapcase() -> string
	    
    Return a copy of the string S with uppercase characters
    converted to lowercase and vice versa.
>>>			


-mtw

-- 
Matthew White - District Systems Administrator
Tigard/Tualatin School District
503.431.4128

"The greatest thing in this world is not so much where we are, but in
what direction we are moving."   -Oliver Wendell Holmes



More information about the Tutor mailing list