Format Strings in Python

We have very powerful method str.format() in python through which you can do variable substitutions and value formatting.
Let’s understand the usage of format with couple of examples.

 
ex1

Formatters work by putting in one or more replacement fields and placeholders defined by a pair of curly braces { } into a string and calling the str.format(). The value we want to put into the placeholders and concatenate with the string passed as parameters into the format function.

format() method takes any number of parameters. But, is divided into two types of parameters:
Positional parameters – list of parameters that can be accessed with index of parameter inside curly braces {index} ,See the example below.

ex2

Keyword parameters – list of parameters of type key=value, that can be accessed with key of parameter inside curly braces {key}
See the example :
ex3

we have another method of formatting string literals , as shown below:

ex6

You could also call functions as shown below:

ex7

Hope you have enjoyed the post,please share your comments

Advertisements

Introduction to Python

From today, we will start learning the most powerful and dynamic programming language popular for web development,big data, data science,and scripting.

Why Python:
1.You can create solutions quickly and others can understand them easily
2.Its an open source tool and has a great community when ever you are in trouble
3. No need of compilation
4.Cross platform,Runs anywhere, including Mac OS X, Windows, Linux, and Unix,
5.Comes with a large standard library that supports many common programming tasks such as connecting to web servers, searching text with regular expressions, reading and modifying files.
6.Is easily extended by adding new modules implemented in a compiled language such as C or C++.

You can find the more information here:
https://wiki.python.org/moin/BeginnersGuide/Overview

How to download Python:

You can download the Python Shell, using the below link
https://www.python.org/
Go to the above site, under downloads /Select OS type and down the latest version of Python ( 3.7.1)

After installing the exe, you can see the IDLE (Python 3.7) under Start->programs
or you can start the Python, by following the below steps
Go to Windows power shell program, and Type Python as shown below
Open python via Windows powershell

If you want to come out of Python, Press Control+z and Enter

We have many IDEs through which we can write Python code, I have listed top 5

1.PyCharm
2.Spyder
3.Eclipse+PyDev
4.IDLE – Which we have downloaded from above website
5.Atom

You can find more details about this in the below site
https://realpython.com/python-ides-code-editors-guide/

lets Finish this blog post with Simple Hello world Program.

Open Python IDLE and type print

first Python program

As you see python is a case sensitive language.

Thats it for now and we will continue to learn more about python in detail in upcoming posts.