String methods

String are example of Python objects. An object contains both data as well as methods, which are effectively functions which that are built into the object and are available to any instance of the object. Python has a function called dir that lists the methods available for an object. The type function shows the type of an object and the dir function shows the available methods.
>>> word = 'Hello world'
>>> type(word)

>>> dir(word)
['__add__', '__class__', '__contains__', '__delattr__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', '__getnewargs__', '__getslice__', '__gt__', '__hash__', '__init__', '__le__', '__len__', '__lt__', '__mod__', '__mul__', '__ne__', '__new__', '__reduce__', '__reduce_ex__', '__repr__', '__rmod__', '__rmul__', '__setattr__', '__sizeof__', '__str__', '__subclasshook__', '_formatter_field_name_split', '_formatter_parser', 'capitalize', 'center', 'count', 'decode', 'encode', 'endswith', 'expandtabs', 'find', 'format', 'index', 'isalnum', 'isalpha', 'isdigit', 'islower', 'isspace', 'istitle', 'isupper', 'join','ljust', 'lower', 'lstrip', 'partition', 'replace', 'rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split', 'splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate', 'upper', 'zfill']
Calling a method is similar to calling a function—it takes arguments and returns a value—but the syntax is different. We call a method by appending the method name to the variable name using the period as a delimiter. For example, the method upper takes a string and returns a new string with all uppercase letters. Instead of the function syntax upper(word), it uses the method syntax word.upper().
>>> word
'Hello world'
>>> new_word=word.upper()
>>> print new_word
HELLO WORLD
The form of dot notation specifies the name of the method, upper, and the name of the string to apply the method to, word. The empty parenthesis indicates that this method takes no arguments. A method call is called an invocation; in this case, we would say that we are invoking upper on the word.
If we want to find specific character in a string we would have to use method find. This method searches for the position of the one character within a string.
>>> word = ‘hello’
>>>index = word.find(‘l’)
>>>print index 
2
As you can see this method finds the position at which that character appears for the first time  in a string and in this case it’s 2nd position. The find method can find substrings, not just characters.
>>>word.find(‘lo’)
3 
It can take as a second argument the index where it should start:
>>>word.find(‘lo’,2)
3
A common task that is very useful in order to remove white space from the beginning and end of a string is accomplished using strip method:
>>> line = ' Here we go again '
>>> line.strip()
'Here we go again'
The method startswith is used to check if the line starts with specific set of substrings or characters. So to apply this method we have to define an argument. The return value is Boolean value (True or False)
>>> line = 'Here we go again'
>>> line.startswith('Here')
True
Startstwith method requires case to match so sometimes it’s better to apply lower method to lower any uppercase letters before we do any checking.
>>> line = 'Here we go again'
>>> line.startswith('h')
False
>>> line.lower()
'here we go again'
>>> line.lower().startswith('h')
The lower method is called because startswith method couldn’t find h letter in a string ‘Here we go again’ although we’re absolutely sure that h is in this string. After lowering uppercase letters in a string we’ve found the h letter and the Boolean value is True.
 Exercise – Use count method to count how many times ‘e’ letter is in the string ‘Here we go again’.
>>>line = ‘Here we go again’
>>>line.count(‘e’)
3

Nema komentara:

Objavi komentar