Regular Expressions in Python

Regular Expressions in Python

What is a Regular Expression? It’s a string pattern written in a compact syntax, that allows us to quickly check whether a given string matches or contains a given pattern. The power of regular expressions is that they can specify patterns, not just fixed characters. Many examples in this article can be found on: Googles Python Course Python Programming – Pattern Matching The Odds & Ends of Python Regular Expressions Introduction to Python Regular Expressions Basic patterns a, X, 9 ordinary characters just match themselves exactly. . ^ $ * + ? { [ ] | ( ) meta-characters with special meanings (see below) . (a period) matches any single character except newline ‘n’ w matches a “word” character: a letter or digit or underbar [a-zA-Z0-9_]. It only matches a single character not a whole word. W matches any non-word character. w+ matches one or more words / characters b boundary between word and non-word s matches a single whitespace character, space, newline, return, tab, form S matches any non-whitespace character. t, n, r tab, newline, return D matches anything but a digit d matches a decimal digit [0-9] d{1,5} matches a digit between 1 and 5 in lengths. {n}…

How to use the Random Module in Python

How to use the Random Module in Python

Overview In this post, I would like to describe the usage of the random module in Python. The random module provides access to functions that support many operations. Perhaps the most important thing is that it allows you to generate random numbers. When to use it? We want the computer to pick a random number in a given range Pick a random element from a list, pick a random card from a deck, flip a coin etc. When making your password database more secure or powering a random page feature of your website. Python Programming – The Random Module How to Generate a Random Number in Python | Python Program to Generate Random Numbers Number Guessing Game in Python Random functions The Random module contains some very useful functions. Randint If we wanted a random integer, we can use the randint function Randint accepts two parameters: a lowest and a highest number. Generate integers between 1,5. The first value should be less than the second. import random print random.randint(0, 5) This will output either 1, 2, 3, 4 or 5. Random If you want a larger number, you can multiply it. For example, a random number between 0 and 100:…

Queue in Python

Queue in Python

You must have seen queues in real life while waiting for an appointment to doctor or while ordering food in a restaurant. The queue data structure follows last in first out (LIFO) order for accessing elements. The element which was added first can only be accessed or deleted. In this article, we will study the underlying concept behind queue data structure and implement it in python. How to implement queue in python? Queue is a linear data structure in which we can only access or remove the element which was added first to it. We will implement a queue using a list. For implementation, we will define a queue class which will have a list to contain the elements and a queueLength field to contain the length of the list. The Queue class implementation in python will be as follows. class Queue: def __init__(self): self.queueList=list() self.queueLength=0 Add element to a queue in python When we add an element to a queue, the operation is termed as enqueue operation. To implement the enqueue operation, we will just append the element to the list in the queue. Then, we will increment the queueLength by one. The enQueue() method to implement the enqueue…

String Splicing in Python

String Splicing in Python

Python strings are sequences of characters enclosed in single, double or triple quotes. Strings are immutable in python. We can access each character of a string using string splicing in python. Splicing is also termed indexing. What is String Splicing in Python? String splicing or indexing is a method by which we can access any character or group of characters from a python string. In python, characters or sub strings of a string can be accessed with the help of square brackets [ ] just like we access elements from a list in python. We can access a character from a string using positive indices as well as negative indices. When using positive indices for string splicing in python, the first character of the string is given index zero and the index of subsequent characters are increased by 1 till end. Python Programming – String Slices How to remove punctuation from a Python String How to split string variables in Python For example, we can print first character, third character and eleventh character of an string using the following program. Note that indexing is 0 based in python. i.e. first character is given the index 0 and not 1. myString=”pythonarray” x=myString[0] print(x) x=myString[2]…

Split a number in a string in Python

Split a number in a string in Python

While processing text data, it may be a situation where we have to extract numbers from the text data. In python, we process text data using strings. So, the task we have to do is to find and split a number in a string. While extracting the numbers, we can classify the string into two types. The first type will contain only numbers which are space separated and the second type of strings will also contain alphabets and punctuation marks along with the numbers. In this article, we will see how we can extract numbers from both the types of strings one by one. So, let’s dive into it. How to split string variables in Python Python String Methods for String Manipulation How to Join Strings in Python 3 Split a number in a string when the string contains only space separated numbers. When the string contains only space separated numbers in string format, we can simply split the string at the spaces using python string split operation. The split method when invoked on any string returns a list of sub strings which will be numbers in string format in our case. After getting the numbers in the string format in the…

How to Join Strings in Python 3

How to Join Strings in Python 3

Programmers are destined to work with plenty of string data. This is partial because computer languages are tied to human language, we use one to create the other, and vice versa. For this reason, it’s a good idea to master the ins and outs of working with strings early on. In Python, this includes learning how to join strings. Manipulating strings may seem daunting, but the Python language includes tools that make this complex task easier. Before diving into Python’s toolset, let’s take a moment to examine the properties of strings in Python. Encoding and Decoding Strings (in Python 3.x) Python String Methods for String Manipulation String to Integer in Python A Little String Theory As you may recall, in Python, strings are an array of character data. An important point about strings is that they are immutable in the Python language. This means that once a Python string is created, it cannot be changed. Changing the string would require creating an entirely new string, or overwriting the old one. We can verify this feature of Python by creating a new string variable. If we try to change a character in the string, Python will give us a Traceback Error.…

Web Scraping with BeautifulSoup

Web Scraping with BeautifulSoup

Web Scraping “Web scraping (web harvesting or web data extraction) is a computer software technique of extracting information from websites.” HTML parsing is easy in Python, especially with help of the BeautifulSoup library. In this post we will scrape a website (our own) to extract all URL’s. Getting Started To begin with, make sure that you have the necessary modules installed. In the example below, we are using Beautiful Soup 4 and Requests on a system with Python 2.7 installed. Installing BeautifulSoup and Requests can be done with pip: $ pip install requests $ pip install beautifulsoup4 What is Beautiful Soup? On the top of their website, you can read: “You didn’t write that awful page. You’re just trying to get some data out of it. Beautiful Soup is here to help. Since 2004, it’s been saving programmers hours or days of work on quick-turnaround screen scraping projects.” Scraping websites with Python Beautiful Soup 4 Python HTML Parser: How to scrape HTML content | Parsing HTML in Python with BeautifulSoup Beautiful Soup Features: Beautiful Soup provides a few simple methods and Pythonic idioms for navigating, searching, and modifying a parse tree: a toolkit for dissecting a document and extracting what you need. It doesn’t take much…

Using pywhois for retrieving WHOIS information

Using pywhois for retrieving WHOIS information

What is pywhois? pywhois is a Python module for retrieving WHOIS information of domains. pywhois works with Python 2.4+ and no external dependencies [Source] Installation The installation of pywhois is done through the pip command. pip install python-whois Now when the package is installed, you can start using it. Remember, that you have to import it first. import whois pywhois Usage We can use the pywhois module to query a WHOIS server directly and to parse WHOIS data for a given domain. We are able to extract data for all the popular TLDs (com, org, net, …) Extract a specific word from a string in Python How to Comment Inside a Python Dictionary Python Code Examples pywhois Examples On the pywhois project website, we can see how we can use pywhois to extract data. Let’s begin by importing the whois module and create a variable. >>> import whois >>> w = whois.whois(‘pythonforbeginners.com’) To print the values of all found attributes, we simple type: >>> print w The output should look something like this: creation_date: [datetime.datetime(2012, 9, 15, 0, 0), ’15 Sep 2012 20:41:00’] domain_name: [‘PYTHONFORBEGINNERS.COM’, ‘pythonforbeginners.com’] … … updated_date: 2013-08-20 00:00:00 whois_server: whois.enom.com We can print out any attribute we want. Say,…

Tweet Search with Python

Tweet Search with Python

Overview Twitter’s API is REST-based and will return results as either XML or JSON, as well as both RSS and ATOM feed formats. Public timelines can be accessed by any client, but all other Twitter methods require authentication. About this script The program is well documented and should be straightforward. Open up a text editor, copy & paste the code below. Save the file as: “tweet_search.py” and exit the editor. Bitly URL Shortener using Python Parse JSON objects in Python Python Script to Get the Geo Location of an IP Address Getting Started Let’s take a look at the program below that we call tweet_search.py #!/usr/bin/python import json import sys import urllib2 import os usage = “”” Usage: ./tweet_search.py ‘keyword’ e.g ./tweet_search.py pythonforbeginners Use “+” to replace whitespace” e.g ./tweet_search.py “python+for+beginners” “”” # Check that the user puts in an argument, else print the usage variable, then quit. if len(sys.argv)!=2: print (usage) sys.exit(0) # The screen name in Twitter, is the screen name of the user for whom to return results for. # Set the screen name to the second argument screen = sys.argv[1] # Open the twitter search URL the result will be shown in json format url =…

How to use urllib2 in Python

How to use urllib2 in Python

Overview While the title of this posts says “Urllib2”, we are going to show some examples where you use urllib, since they are often used together. This is going to be an introduction post of urllib2, where we are going to focus on Getting URLs, Requests, Posts, User Agents and Error handling. Please see the official documentation for more information. Also, this article is written for Python version 2.x HTTP is based on requests and responses – the client makes requests and servers send responses. A program on the Internet can work as a client (access resources) or as a server (makes services available). An URL identifies a resource on the Internet. Python Modules Urllib2 User Agent Fetching data from the Internet Google Command Line Script What is Urllib2? urllib2 is a Python module that can be used for fetching URLs. It defines functions and classes to help with URL actions (basic and digest authentication, redirections, cookies, etc) The magic starts with importing the urllib2 module. What is the difference between urllib and urllib2? While both modules do URL request related stuff, they have different functionality urllib2 can accept a Request object to set the headers for a URL request, urllib accepts…