Python is extremely useful for data science. It is no surprise then, that aspiring data analysts should learn it’s basics to effectively work with data. Python is a high-level programming language of choice for data science. It overtook R,  another programming language and free software environment for statistical computing, in recent years. In 2018 66% of data scientists said they use it daily, making this language the number one tool for analysts. Python is relatively easy to learn, even for beginners.

Python’s Usage

Various applications of Python can really help you in almost every field you work in. You can choose from:

  • web programming,
  • data analyzing and visualization,
  • scientific and mathematical computing,
  • software applications
  • and many others.

Python supports a variety of libraries and modules that reduce the length of the written code and encourage users for taking up new interesting projects.

Python in Data Analytics

This language is used in data analytics as a tool that helps to access databases and work with them. It is useful while importing and exporting data, also while using web scraping techniques. Finally, it helps to clean and prepare data sets.

Python also allows us to build plots and visualizations to understand data and derive valuable insights. It is easier to drive insights when you can see graphs, pie charts and other formats than just many numbers.

Python copes deals with unstructured data, big data sets and makes the work of data analyst easier.

In data analytics, pandas and numpy are the mostly used libraries. Matplotlib and bokeh are useful for graphs’ creation like the ones you can design in Excel and Google Sheets.

Python Formatting

Formatting in Python concatenates elements within a string through positional formatting. It helps to deal with text, e.g. to input forms from a website, extract data from processed text and perform activities such as sentimental analysis. Python is also useful when you need to control the formatting of output in a more detailed way than simply printing space-separated values.

There are a few methods of presenting the output of a program – data can be printed in an easily readable form, or written to a file for future use. There are several ways to format an output:

  • the str.format() method of strings that helps you to get an output
  • the formatted string literals (f-strings)
  • you can do all string handling by using string slicing and concatenation operations to create all the needed layouts. The string type has a few methods that perform useful operations for padding strings to a given column width.

Python String Formatting – How to Do It Easily!

While coding in Python, we often struggle with the formatting of strings. Regardless of preferences, everyone wants to be able to use it in an efficient and convenient way.

Fortunately, up to the Python 3.6, we have a possibility to work with f-Strings – a new string formatting mechanism known as Literal String Interpolation. The idea behind it is to make string interpolation simpler and easier to use. To create an f-string, you should prefix the string with the letter ‘f’. It can then be formatted in the same way than str.format(). F-strings provide a concise and convenient way to embed python expressions inside string literals while formatting.

When There Is No f-Strings…

Before we discuss the new functionality, let us remind some other common ways of formatting strings: % operator and format() method.

1. % Operator

The oldest version of string formatting, with the use of it we can do simple string interpolation:

It was quite easy. Let’s try to make it more complicated.

As we can see above, to put more than one variable in the string,  we need to use tuple of variables in the % operator.

However, as we can notice with the increase of the number of variables, the readability rapidly decreases. In case we use several or more variables, it is very easy to get lost. Additionally, if we are not a single person who uses the written code, it is pretty sure that the other person will spend a lot of time understanding the meaning of the messy code.

Due to that fact, the mentioned % operator works best in cases with a small number of variables, when complicated string interpolations are redundant.

2. Format() Method

The next way of string formatting was introduced as an improved version of  % operator. It is somehow similar to it:


With the use of format() method we should not even worry about the order of given variables. Within curly brackets we can use unknown variables (s1 and s2) and specify their values within the format() method.

Let’s use another example. First of all, let us define a simple function (we assume pet_no>0):

We can use another way of providing variables – we put their indices from format method into curly brackets:

Unluckily, in that case, we also struggle with poor readability. The more variables we have in the string, the less legible the code.

3. Template Strings – Simple But Useful in case of User Input

Template Strings are a good solution when you allow users to introduce their own input to your applications.

The first step is to import Template class from string module. The following example shows a sample usage of Template.

Template Strings are less sophisticated and simpler in use then f-strings, because they don’t allow to use format specifiers. Thus, this solution is dedicated to those cases in which both simplicity is important and values are inserted by users.

Savior of Pythonists – f-Strings

Finally, there comes awaited f-Strings. The improved string interpolation method is fast, readable and easy to use. With the use of curly brackets {}, it allows the user to directly place string variables, other strings or even calculations within a string! The only requirement is to add the f prefix at the beginning of the string:

Isn’t it great? Within one f-String we can mix interpolation of constants, calculations, functions and methods.

Let’s try with calling a defined function:

As we can see, it is very useful to provide the results of various functions simply within the string.

Updates in Python 3.8 for f-strings

Python 3.8 is approaching its official release date and together with this release, we should also expect  some interesting news associated with f-strings.

python, python 3.8

F-strings will now have new specifier “=” which can print expression and also expression value – one useful feature for debugging purposes.

For example:

As you can see, if you add = specifier, then f-string is printing not only the result but the full expression. You have the possibility to check the values of every variable and easier catch possible mistakes.

Possibility of Choice

At that moment, we have the three above discussed ways of formatting strings. Of course, we can use them interchangeably or conveniently choose one of them for a specific situation. However, if you would like to make your coding easier, I strongly recommend using f-Strings!

Python – to Sum Up

As you can see, Python is really useful in data analytics and has many different applications. It has a couple of different methods of formatting that can be used for our needs and at our convenience. I highly recommend using f-strings as a really easy way to achieve what we want using Python in data analytics. It is simple and can speed up your work.