Aug 12, 2019

Format strings | Python programming

Format strings are used to format the printing output. We can print a string with variables in it. You know we can print a normal string with print() function. In our printing tutorial we saw that print() function accept a string as an argument . Do you know , we can supply that string with embedded variables. First of all take a look at following script.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
x = 3
print ("value of x is %d" % x)
print ("value of x+3 is %d"% (x+3))

[email protected]:~/programming$ python tmp1.py
value of x is 3
value of x+3 is 6

[email protected]:~/programming$ python3 tmp1.py
value of x is 3
value of x+3 is 6
Give your focus on following line of code. print ("value of x is %d" % x) You know that we indicate a string by adding two " s around it. But there is a %d inside the string. When print() function see it , the function don't print it and it expect a decimal number after the string. Yes you are correct, d stands for a decimal. Now after the string we add another % symbol and put our integer variable there. So when print() function see %d it assume we are going to print a decimal in string. So it will check for a decimal after string. It will pickup that decimal and put it on the right place in string. After it will printed in correct format. Not only decimals. We can print any variables like string, floats etc.Also we can add any number of variables in a string at once. Let's see how.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
x = 5
y = "HacksLand"
print ("our variable string is %s " % y)
print ("integer is %d while string is %s " % (x , y))

[email protected]:~/programming$ python tmp1.py
our variable string is HacksLand 
integer is 5 while string is HacksLand 

[email protected]:~/programming$ python3 tmp1.py
our variable string is HacksLand 
integer is 5 while string is HacksLand 
Hear we have another example of format strings with decimals.
[email protected]:~/programming$ cat tmp1.py
#!/usr/bin/env python
a = 3
b = 4
print ('Sum of %d and %d is %d ' %(a , b , a+ b))

[email protected]:~/programming$ python tmp1.py
Sum of 3 and 4 is 7 

[email protected]:~/programming$ python3 tmp1.py
Sum of 3 and 4 is 7 
OK. Its all about format strings for this tutorial. This techniques will be used in next tutorials.
Jul 03
Reverse TCP shell with Metasploit

Metasploit is an awesome tool. It can be used to automate the exploitation process, generate....

Aug 12
Inputs in C

In this tutorial I'll explain you how to get an input from the user in C language. You know that in....

Feb 15
Introduction to Assembly language

Assembly is a low level programming language. You already know that low level programming languages....

Replying to 's comment Cancel reply
CATEGORIES
ABOUT AUTHER
Thilan Danushka Dissanayaka

Thilan Dissanayaka

Hi, I'm Thilan from Srilanka. An undergraduate Engineering student of University of Ruhuna. I love to explorer things about CS, Hacking, Reverse engineering etc.

SOCIAL
RANDOM ARTICLES