Effective usage of python for server automation needs a little bit of basics, to help you go a long way. In this post the the fundamentals of Python scripting has been discussed.
Check python and its version.
$ python --version
Just run ‘python’ to go to python prompt.
$ python
- Variables does not need declaration
- Python supports the following data types:
- boolean, integer, float, long, string, list, object, none
- Variables can be set globally or locally inside a function. In case both are used, the local ones takes precedence.
var1 = 1 Var2 = text
print can output variables of datatypes mentioned earlier.
print "This is a string" print 'this is also a string with single quotes'
This is a string this is also a string with single quotes
print "abc\ndef"This prints abc on first line and inserts a new line to print def.
abc def
print r"abc\ndef"This time it ignores the expansion between the quotes and prints everything, thus,
abc\ndef
Raw strings can be assigned directly, example: string=r’abc\ndef’sometext”
Printing the above string would print abc\ndef’sometext’. Please note, \n and single quotes will be escaped and printed in the output.
Assign input from user to a variable.
city = raw_input("Please enter your City : ") age = int(raw_input("Enter your age : ")
Please enter your City : Brussels Enter your age : 28
To print the string and integer together in a statement, use the str casting fuction. str(integer_variable).
‘+’ concatenates and ‘,’ separates two variables with a space.
print "your City is" + city + "And your Age is" + str(age) print "your City is",city,"And your Age is", str(age)
your City isBrusselsAnd your Age is 28 your City is Brussels And your Age is 28
print len("hello world")
returns the length of the string “hello world”
11
a=("abc") print len(a)
3
Find sub-string inside a string.
print "abcd".find("a")returns 0. Because 0 is the index/position of substring “a”.
[/python]
0
print "abcd".find("b")returns 1. Because 1 is the index/position of substring “b”.
[/python]
1
print "-".join("abc") print ".".join("abc")
concatenates the characters of string “abc” with the specified connector.
a-b-c a.b.c
Convert string from Uppercase to Lowercase and vice-versa.
'THIS TEXT'.lower() 'this text'.lower()
this text THIS TEXT
print "text1 text2 text3" print "text1 text2 text3".replace("text2","null")
Search for “text2” and Replace with “null”
text1 text2 text3 text1 null text3
print "text1 text2 text3".split()
prints a List of all words from inside the string
['text1', 'text2', 'text3']
x="a b c".splt()
Each item in the list can be called with its index. print x[0] will return “a” and print x[1] returns “b” and so on.
print x[0] print x[1]
a b
Creating an List nammed arr_var.
arr_var=[1, "text1", 3] print arr_varprints,
[1, 'text1', 3]
Add or Remove items to an List
arr_var.append('text2') print arr_varadds ‘text2’ to arr_var
[1, 'text1', 3, 'text2']
arr_var.pop('0') print arr_varremoves the 1st item on the list.
1 ['text1', 3, 'text2']
arr_var.pop('2') print arr_varremoves the 3rd item on the list.
text2 ['text1', 3]
Create List with range
print range(5)prints an List of all numbers ranging from 0 to 5
[0, 1, 2, 3, 5]
Multidimensional LISTS are LISTS inside LISTS.
a=[[0,1,2],[3,4,5]] print a[0]
Prints the first sub-List.
[0, 1, 2]
print a[0][1]prints the 2nd element of the 1st sub-List
1
print a[0][2]prints the 3rd element of 1st sub-List
2