Get the App
SLTechnology News&Howtos  ›  Development  › 

What is the use of format function in python

Shulou Source: shulou.com Published: 2022-06-01 03:06:47 10月04日 Update

This article mainly shows you "what is the use of the format function in python", the content is simple and clear, and I hope it can help you solve your doubts, so let me lead you to study and learn "what is the use of the format function in python" this article.

Format usage

Basic usage

Anyone who has a basic knowledge of other languages should be more or less exposed to format string substitution. Here, let's take a direct look at the basic ways to use it:

Print ("My name is {}" .format ("liyuanjing")) # curly braces match, fill print ("My {1} is {0}" .format ("liyuanjing", "name")) # numeric matching sequentially, fill print ("My {name} is {tom}" .format (tom= "liyuanjing", name= "name")) # keyword matching by position, and fill in advanced usage by keyword

The format function can not only match the replacement string, but also use it to match its text, or take a few decimal places, and so on. Next, let's take a look at how these uses are implemented.

Print ('{} and {} '.format (' tom', 'Jerry')) print (' {: 10s} '.format (' *')) # default left alignment print ('{: > 10s} '.format (' *')) # right alignment print ('{: ^ 10s} '.format (' *')) # middle alignment print ('{:

Tags: Functions content characters strings articles keys keywords decimals numeric values symbols higher-order learning help things binary position octal hexadecimal decimal available through the Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno NVidia Shulou Information MySQL Docker Shulou Technology