Formatting in Python

How to format string in Python and link values to them

Girolamo Pinto
5 min readApr 8, 2022
Photo by Mohammad Rahmani on Unsplash

Welcome back, fellow developers.

Here we are with another beginners guide about Python. In this guide we’re going to present some ways to format string in Python and to link our variables to them. In this way we can display on the screen all the informations we need in just one string.

There are many ways to put all the informations in one string. Today we will see four methods and you could choose your favourite method.

Typecasting

The first method I propose to you is the casting method. Don’t worry, you shouldn’t perform a piece on a stage.

Casting is when you convert a variable value from one type to another. That is, in Python, done with functions like int() or float() or str(). A pattern is that you convert a number as a string into a number.

The practice of converting the value of a single data type (integer, string, float, etc.) to a different data type is known as type conversion. Python includes two types of type conversion:

  • Explicit Conversion
  • Implicit Conversion

Explicit Conversion

In Explicit Type Conversion, users convert the data type of the item to needed data type. We use the predefined roles such as int(), float(), str(), etc to execute explicit type conversion.

This type of conversion is also called typecasting because the user casts (change) the data type of the objects.

Let’s make an example. As you know we can concatenate strings and put them together by using the + (plus) operator. But if we try to concatenate other types of values, we’ll get an error.

As you can see an exception raised telling us that we can only concatenate strings values. In our help it comes the typecasting, or simpler casting. What we’re going to do, is to convert all these values, integers and floats, in string values.

As you can see, we converted our integer and float values in strings. Obviously we don’t need to convert the variable dog_name because it’s a string value already.

So the str() operator can help us to show all the informations we need in just one string, without converting by hand the values written before. We can do this process also with other data types:

Implicit Conversion

In implicit conversion, Python automatically converts one data type to another data type. This process doesn’t need any user involvement. To be honest, this type of conversion is useless for what we’re talking in this guide, but it’s a good thing knowing about it.

As you can notice, from the code above, we created two variables num_int and num_float, respectively an integer value and a float value, preserving their sum in another variable num_new.

From the output, we may observe the datatype of num_int is an integer, the datatype of num_float is a float thanks to type() method.

Additionally, we may observe the num_new has float data type as Python consistently transforms smaller data type to bigger data type to prevent the lack of data.

Old school Formatting

I call this method an old school method, because it’s a C-like method and for those who are experienced with C-like programming languages, it could sound very familiar.

Python uses C-style string formatting to create new, formatted strings. The % operator is used to format a set of variables enclosed in a tuple (a fixed size list), together with a format string, which contains normal text together with argument specifiers, special symbols like %s and %d.

Let’s take the example above to try ourselves this new method.

As you can see we reduced a lot the code written and we didn’t use the casting to tell Python the type of data of each value. This is very useful when we have a lot of data stored and we don’t want to make a casting on these data.

Note : the symbol %.2f it represents the float value, the dot and the number represent the number of digits we want after the comma; by default there are six digits.

New school Formatting

Python offers to developers another way to link strings and other data types, a modern way. By using the format() method. In this way we can better specify what kind of values we link to the string and we don’t need to remember any kind of symbols, but we just need to type the value.

Let’s make an example.

As you can see we used curly brackets to interpolate the value we expect and in format() method we associated that value with the corresponding variable.

Note : in curly brackets we can use the name we want for the value.

Formatting in Python 3.6 and above

This is my preferred method, because it’s easy and fast but, rather than the new school method, here we should necessarily use the name of the variables we stored.

Let’s try it out.

As you can notice we just need to put a f character before quotation marks to tell the compiler that we’re formatting the string and inside the curly brackets we use the same name that we used to store variables before.

🖐Well, that’s all for this beginners guide. I hope that you found this guide useful.

For any further clarification, write down in the comments section, I’ll be glad to answer you.

🍕Follow me on my socials: instagram, facebook, linkedin and github.

📧Mail me at: girolamopinto1@gmail.com

--

--

Girolamo Pinto

Computer Engineer student. Python developer. C# and Unity addicted. Music, food and Formula 1 lover.