String formatting in Python Programming Language | applied electronics engineering


String formatting in Python Programming Language

By Applied Electronics - Friday, May 13, 2016 No Comments
String formatting in Python is similar to the string formatting in c, c++, c#. Generally, string formatting is a way embedding variables into a string for display. The variable could be of any data types - numeric, string, boolean etc. In python, it is more than just embedding variable and printing for display. In python programming language, it can be used to perform string processing and save the result in some variable name instead of printing(like printf statement in C).

Moreover, we can identify or classify 3 techniques for string formatting in Python. One is using %s in the string expression. The expression may use %s or %d etc in order to put strings and numbers into the string. The second one uses a build in method called format( ) wherein strings and numbers to be put into the desired string is passed as argument to the format( ) method. The 3rd one is a mixture of 1st and 2nd approaches.

String formatting in Python Programming Language

Using %s in expression

 Let's look quickly into a simple example:

>>> 'the %s house address is %d'%('red',112)
'the red house address is 112'

Or assign to a variable object:

>>> s = 'the %s house address is %d'%('red',112)
>>> s
'the red house address is 112'

 You can also print the statement as in C programming language:

>>> print('the %s house address is %d' %('pink',54))
the pink house address is 54

In C you would have:

int x = 3;
printf("my number is %d", x);

note: % is called specifier or formatting operator

Thus like in C programming language, Python has formatting characters for numeric, strings, characters etc.
For string you would use s, for integer you would use d, for floating points you would use e(exponential form), f or g.

 Integer string formatting example

Use d or i for integers. See example:

>>> 'this is d form = %d and this is i form = %i' %(4.56, 4.56)
'this is d form = 4 and this is i form = 4'

Floating point number string formatting

Use e, f or g formatting character:

>>> 'this is e form = %e, this is f form= %f and this is g form =%g' %(2.634,2.634,2.634)
'this is e form = 2.634000e+00, this is f form= 2.634000 and this is g form =2.634'

You can also control the position and decimal points in the output using the following formatting rule

%<width>.<decimal point><formatting char>
eg %2.4f
eg %4d

As an example of the position or width control see the following example:

>>> 'x = %d' %11
'x = 11'
>>> 'x = %1d' %11
'x = 11'
>>> 'x = %4d' %11
'x =   11'
>>> 'x = %10d' %11
'x =         11'
>>> 'x = %30d' %11
'x =                             11'

In case of decimal, there is no need for decimal point.

For floating see the examples below:

>>> 'y = %0.1f' %3.17495721376
'y = 3.2'
>>> 'y = %0.2f' %3.17495721376
'y = 3.17'
>>> 'y = %0.3f' %3.17495721376
'y = 3.175'
>>> 'y = %0.4f' %3.17495721376
'y = 3.1750'
>>> 'y = %0.7f' %3.17495721376
'y = 3.1749572'

Last words- instead of using %d, %f and so on we can use %s for all the formats. Very easy way!

>>> s = 'Hi %s, your id is %d' %('Micheal', 301)
>>> s
'Hi Micheal, your id is 301'

>>> s = 'works also for floating point %s' %2.1542
>>> s
'works also for floating point 2.1542'

 Yet there is another technique for string formatting. It uses method technique which is described below.

String formatting using Methods

An alternative to expression string formatting is the method string formatting. In method string formatting, we create the string/object(s = '......{ }.....{ }') and we place the position or key place holder slots { } where we want our numbers or strings or characters to appear. We assign position or key inside the slot {}-for example for position: {0}, {1}, {2}...or keys: {name}, {address} etc. Then we use method called format and pass the values we want to appear in the slots as arguments.

Positional format method

>>> s = 'Hi {0}, your id is {1}'
>>> s.format('Micheal',438)
'Hi Micheal, your id is 438'

 Key format method

>>> s = 'Hi {name}, your id is {number}'
>>> s.format(name='Richie',number = 78)
'Hi Richie, your id is 78'

Using both positional and key method

>>> s = 'Hi {name}, your id is {0}'
>>> s.format(21, name='John')
'Hi John, your id is 21'

 In the format method, the first argument must be positional argument.

Using relative position:

>>> s = 'Hi {}, your id is {}'
>>> s.format('Hari',81)
'Hi Hari, your id is 81'

With this string formatting in python programming language, the chapter on string is completed. Next python tutorial will be on List in Python programming language which is one major causes for the power-fullness of Python programming language.

No Comment to " String formatting in Python Programming Language "