10 Print Formatting With Strings in python || Advanced String Formatting In Python
In this Python tutorial, we will be learning how to perform some advanced string formatting operations. Formatting our strings allows us to display our information in exactly the way we would like it to be displayed. Everyone, in almost all areas of Python programming, comes across a situation where they need to format a data type in a specific way. Let's get started.
Advanced String Formatting In Python
In this Python tutorial, we will dig deeper into the Python formatting string method. We can perform a lot of tasks using the Python formatting string method in this tutorial, we will cover some of the most used features of this method but we cannot cover them all. If you would like to see the full list of features visits the Python documentation.
In the previous tutorial, we went over the basics of the Python formatting string method which gave us a good understanding of the power of string formatting in Python. When using formatting we can modify existing strings to help our programs to be more flexible and allows us to adapt our strings to fit certain situations.
Positions Content Using String Formatting in Python
We can use string formatting to position our content within a string just like the ljust string method, center string method and rjust string method. Let's Take a look at this in action.
Left Justify String Formatting Method
'{:40}'.format('Left')
'Left '
Left Justify String Formatting Method Explained
'{:40}'.format('Left') - In this example we add our placeholder({}) inside the curly brackets we insert colon(:) which is part of the string formatting syntax and then we add a carrot() pointing to the left which indicates we want to left justify the content. We also include 40 which is how many index spaces we want our new string object to span. The we call the format string method on our string object and provide a string as the argument.
'Left ' - We are returned a new string object that contains our argument and it is left justified with the string object expanding forty index positions.
Right Justify String Formatting Method
'{:40}'.format('right')
' right'
'{:40}'.format('right') - We create a new string object that contains our placeholder({}) inside the placeholder we place a colon(:) which is part of string formatting syntax and we place a carrot() which is pointing to the right which indicates we want to right justify the content. We include 40 which states we want the new line object to span forty index positions.
' right' - We are returned a new string object that contains our argument which is right justified within 40 index spaces.
Center Justify String Formatting Method
'{:40}'.format('center')
' center '
'{:40}'.format('center') - We create a new string object which contains a placeholder({}) and this placeholder contains a colon which is part of the syntax, an up carrot(^) which indicates we want to center our content in the string object. We indicate we want our string to span forty index positions.
' center ' - We are returned a new string object which contains our argument which is centered within our forty index positions.
Binary String Formatting Method
If we would like to display the binary number of a value we can use the binary string format.
How To Get The Binary Number
'{:b}'.format(12331)
'11000000101011'
'{:b}'.format(12331) - We establish a new string object in which we will use to get the binary number of the value. We place our placeholder({}) in the string object. Inside our placeholder, we place the colon(:) which is part of the syntax then we insert a b which will indicate we want the binary representation of our value. Then we call format string method on our string object with the argument of 12331 and this is value we want to know the binary representation of our value.
'11000000101011' - We are our returned a new string object that contains our binary representation of the value 12331.
How To Get The Prefix For A Binary Number
'{:#b}'.format(12331)
'0b11000000101011'
'{:#b}'.format(12331) - In the above example we get the binary number of our value in this example we include a # sign to indicate we want the prefix for a binary number. The rest remains the same.
'0b11000000101011' - We are returned a new string object that contains the binary representation of 12331 but we are also returned the the prefix for binary format which is 'Ob'.
Category
Education
-~-~~-~~~-~~-~-
Please watch: "27 Python Statements Test Solutions"
https://www.youtube.com/watch?v=L53wMIouAUk
-~-~~-~~~-~~-~-
Что делает видео по-настоящему запоминающимся? Наверное, та самая атмосфера, которая заставляет забыть о времени. Когда вы заходите на RUVIDEO, чтобы посмотреть онлайн «10 Print Formatting With Strings in python || Advanced String Formatting In Python», вы рассчитываете на нечто большее, чем просто загрузку плеера. И мы это понимаем. Контент такого уровня заслуживает того, чтобы его смотрели в HD 1080, без дрожания картинки и бесконечного буферизации.
Честно говоря, Rutube сегодня — это кладезь уникальных находок, которые часто теряются в общем шуме. Мы же вытаскиваем на поверхность самое интересное. Будь то динамичный экшн, глубокий разбор темы от любимого автора или просто уютное видео для настроения — всё это доступно здесь бесплатно и без лишних формальностей. Никаких «заполните анкету, чтобы продолжить». Только вы, ваш экран и качественный поток.
Если вас зацепило это видео, не забудьте взглянуть на похожие материалы в блоке справа. Мы откалибровали наши алгоритмы так, чтобы они подбирали контент не просто «по тегам», а по настроению и смыслу. Ведь в конечном итоге, онлайн-кинотеатр — это не склад файлов, а место, где каждый вечер можно найти свою историю. Приятного вам отдыха на RUVIDEO!
Видео взято из открытых источников Rutube. Если вы правообладатель, обратитесь к первоисточнику.