Formatted strings
In this tutorial, we're going to look at formatted strings in Python programming language.
Formatted strings are particularly useful in situations where you dynamically generate some text with your variables. Let me show you. Let's say we have two variables first name and last name. So first we set this to Udhav , and last we set this to Singhal. So let's say with these two variables, we want to generate some text like this. Udhav, in square bracket Singhal is a coder.
Let's say we want to print this on the terminal. How do we do this? Well, we define another variable like message, now here we add the first name, now we need to concatenate this, with a string that contains a space and a square bracket next we need to add a last name, then we need to add a string that contains the closing square brackets followed by is a coder okay?
So, then, if you print message and run this program to see Udhav Singhal is a coder, right? Now,
while this approach perfectly works, it's not ideal because as our text gets more complicated it becomes harder to visualise the output. So someone else reading this code, they have to visualise all the string concatenations in their head. This is where we use formatted strings, they make it easier for us to visualise the output.
So, I'm going to define another variable, let's say msg short for message, and set this to a formatted string. A formatted string is one that is prefixed with an f. So f, quotes. Now in between the quotes, first we want to add the value of the first name variable, so, we add curly braces and here we type first. Next we add a space, we add our square brackets, in between the square brackets, we want to display the last name so once again we add curly braces, and type last, and finally here we type is a coder.
So this is what we call the formatted string. With these curly braces, we're defining place holder or holes in our string, and when we run our program these holes will be filled with the value of our variables.
So here we have two place holder or two holes in our string. One is for the value of our first name variable and the other is for the value of the last name variable. But compare this formatted string with the previous string. With this formatted string we can easily visualise what the output looks like.
Now let's print this other terminal to make sure we get the same exact output. So, let's print message there you go. So Udhav Singhal is a coder.
So to define formatted strings, prefix your strings with an F and then use curly braces to dynamically insert values into your strings.
so thats it for this video
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «Formatted strings», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.