Python Notes-10

In continuation from
Python Notes-9
Python Notes-8
Python Notes-7
Python Notes-6
Python Notes-5
Python Notes-4
Python Notes-3
Python Notes-2
Python Notes-1

Let us solve a real life problem. I want to send email from my gmail account using python. Here is a simple example:


import smtplib
fromaddr = 'user_me@gmail.com'
toaddrs = 'user_you@gmail.com'
msg = 'Stop sending me spams!'
username = 'user_me@gmail.com'
password = 'pwd'
server = smtplib.SMTP('smtp.gmail.com:587')
server.ehlo()
server.starttls()

server.login(username,password)
server.sendmail(fromaddr, toaddrs, msg)
server.quit()

If you use gmail two way authentication you can generate application password from gmail and use that. Later remember to delete the application.

Regular expression

To use Python’s regular expression library you ned to import re


import re
match = re.search('abc','abc: "Always be coding" Title of some blog I read today')

match.group() # prints 'abc'


Let us write find method to make our life easier

def find(pat, data):
  match = re.search(pat, data)
  if match:
    print(match.group())
  else:
    print('Not found!')

find('and','hjkl') #print 'Not found!'

find(r'word:\w\w\w','an example word:cat!!')


The code match = re.search(pat, data) stores the search result in a variable named “match”. Then the if-statement tests the match — if true the search succeeded and match.group() is the matching text. Otherwise if the match is false (None to be more specific), then the search did not succeed, and there is no matching text.

Generally people prefer r in pattern that way you can use special characters in pattern without escaping it so you can use:

find(r':...','rat:.cat') #Looks confusing but first '.' will match actual '.' rest two '.' will match next any two characters.

Justifying Text with rjust(), ljust(), and center()
The rjust() and ljust() string methods return a padded version of the string they are called on, with spaces inserted to justify the text. The first argument to both methods is an integer length for the justified string. Enter the following into the interactive shell:

>>> 'Hello'.rjust(10)
' Hello'
>>> 'Hello'.rjust(20)
' Hello'
>>> 'Hello World'.rjust(20)
' Hello World'
>>> 'Hello'.ljust(10)
'Hello '

An optional second argument to rjust() and ljust() will specify a fill character other than a space character. Enter the following into the interactive shell:

>>> 'Hello'.rjust(20, '*')
'***************Hello'
>>> 'Hello'.ljust(20, '-')
'Hello---------------'

The center() string method works like ljust() and rjust() but centers the text rather than justifying it to the left or right. Enter the following into the interactive shell:

>>> 'Hello'.center(20)
' Hello '
>>> 'Hello'.center(20, '=')
'=======Hello========'

Removing Whitespace with strip(), rstrip(), and lstrip()
Sometimes you may want to strip off whitespace characters (space, tab, and newline) from the left side, right side, or both sides of a string. The strip() string method will return a new string without any whitespace characters at the beginning or end. The lstrip() and rstrip() methods will remove whitespace characters from the left and right ends, respectively. Enter the following into the interactive shell:

>>> spam = ' Hello World '
>>> spam.strip()
'Hello World'
>>> spam.lstrip()
'Hello World '

sentence = “the quick brown fox jumps over the lazy dog”
words = sentence.split()


>>> list('hello')
['h', 'e', 'l', 'l', 'o']

Converting a tuple to a list is handy if you need a mutable version of a tuple value.

To be continued …

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s