A Beginners Guide to Linux Programming with Python - Variables and Data Structures

A Beginners Guide to Linux Programming with Python - Variables and Data Structures
Page content

Text Manipulation

**

#!/bin/python

message = “Welcome to the second part of A Guide to Linux Programming with Python”

print message

Python is very good for solving text-manipulation. Here are some examples:

print message[0] # returns ‘W’

print message[15:20] # returns ‘second’

print message[62:] # returns ‘Python’

You can select a character, or several characters in a string. The # is used for comments. Text manipulation can also be used to do the following:

print message.replace(’the second part’, ‘part 2’)

print message.find(‘Guide’)

The replace method replaces the string (before the comma) found in message with the one after the comma, and the return value is printed. Underneath, the find method returns the position of the sub-string in the string.

Therefore, the following is returned:

Welcome to part 2 of A Guide to Linux Programming with Python

24

Because everything in Python is an object, the string variable has methods also!

As with strings, you can play and manipulate numerical values:

print 5 * 2 + 2.7 # returns 12.7

Again, one does not have to worry about types! :)

You can also multiply a string with an integer.

print 5 * “Python” # returns PythonPythonPythonPythonPython

Data Structures

The better the data structures of a programming language, the more concentration you can devote to coding a solution to a problem. Python has a bunch of extremely useful data structures.

Lists

A list is quite simply a sequence of objects and the objects don’t even have to be the same type. A list is created by listing the items between [ ], while list items are separated by commas.

list = [1, 7, ‘bright’, ‘hub’]

print list[2] # returns ‘bright’

As with text-manipulation with strings, we can manipulate lists.

print list[1:3] # returns [7, ‘bright’]

print list[:3] # returns [1, 7, ‘bright’]

A list’s length can be found using the len() function.

print len(list) # returns ‘4’

Append adds a new item at the end of a list:

list.append(‘rocks’) # will add ‘rocks’ to our list, and returns [1, 7, ‘bright’, ‘hub’, ‘rocks’]

print len(list) # returns ‘5’

Elements can be inserted in a list. We pass the position of where we want to insert the element in to the list, followed by the element we want inserted.

list.insert(2, ‘hey’) # the list now looks like this [1, 7, ‘hey’, ‘bright’, ‘hub’, ‘rocks’]

We can remove elements either by position or value.

list.remove(2) # removes hey

del list[0] # removes element at the beginning of the list (1)

Next Time

In the final part of this series I will summarize “A Guide to Programming Linux with Python” with Tuples, Dictionaries and Sets.

This post is part of the series: A Guide to Linux Programming with Python

Python is a very useful language, whether you need a little script that edits files or want to create a bigger project. Python has all the power you need, including testing, modularization, object-orientation and a massive API. This series will let you get your hands dirty with Python and Linux.

  1. A Guide to Linux Programming with Python
  2. A Guide to Linux Programming with Python - Variables & Data Structures
  3. A Guide to Linux Programming with Python - Tuples, Dictionaries and Sets