S2E4: Unpack Variables & Format FString

Kishan Tongrao
2 min readJan 11, 2022

Hey, my name is Kishan Tongrao. Today, we are going to learn/talk about simple concepts in the Python programming language. This is going to be very short.

Unpack Variables | Iterable Unpacking

In simple words, we can initialize many variables in a single line. It also means we can use Strings, List, Tuples etc to assign values to different variables on a single line. Let’s see that one by one with the below coding example. In Python, we called this Iterable Unpacking.

Unpack Values On Two Variables

Here as we see, we use two values to assign to two variables on the left side in a single line. It looks like a parallel operation.

Unpack List and Tuple

Like variables, we can use list and tuple because they are the best example of iterable. The values of the list or tuple should have an exact count of variables on the left side. Otherwise, it will show us an error.

Unpack String

We can also unpack each character of the string to a variable. Check it out in the below examples.

Store Extra Value In Unpack Operation

If you have a lot of values to unpack and not variables to store we can use ‘_’ and ‘*’. We can use ‘_’ to store unwanted variables and we can use ‘*’ to store multiple values in a single variable.

Format — F String

We can use the Format and F String function to display string or data with a placeholder. We use ‘{}’ as a placeholder. The functionality of both functions is the same. The syntax is different and it is based on your choice of which one to use.

Thank You!

Need more visit : Medium

--

--