String Formatting for Python Learning

  • 2021-12-11 18:28:06
  • OfStack

Directory 1. There are many placeholders 2. Specific usage 1, You can insert several values at a time 2, print floating point number (decimal number) 3, print percentage 4, call dictionary for splicing 5, set split symbol 6 for sep, format symbol 3 for flags. format1, use splicing 2 by default, use index and slice for splicing 3, call ancestor for splicing 4, use dictionary as element library of format 5, use list as element library of format 6, placeholder operator summary

String stitching% s% (% s means 1 string type data str will be inserted)

It means that% s is a placeholder, and the% at the end of the sentence is the value to be filled in before


a = ' I am %s  I 16 Twenty five years old ' %'adam'
print(a) # I am adam  I 16 Twenty five years old 

1. There are many such placeholders

1.% s (str) This is universal and can accept all kinds of data. When it writes floating-point numbers, it does not round up 4 and enter 5

2,% d (int) Insert integer digits

3.% f Floating Point Number Print Floating Point Number (Decimal)

It is best to use them differently, not just% s

2. Specific usage

1. You can insert several more values at a time


b = 'adam'
a = ' I am %s  I %d Twenty five years old ' %(b,16)
print(a) # I am adam  I 16 Twenty five years old 

2. Print floating-point numbers (decimals)

Here, the first two places of f mean to reserve two decimal places


a = ' I %.2f Twenty five years old ' %16.53333 # Keep two decimal places   Will proceed 4 Shed 5 Into 
print(a) # I 16.53 Twenty five years old 

3. Print percentage

Adding 2% after f means showing 1% after the inserted value


a = ' Content %.2f%%' %16.53333
print(a) # Content 16.53 %

4. Call the dictionary for splicing

It populates the placeholder value by indexing key


a = ' I am %(name)s  I %(age)d Twenty five years old ' %{'name':'adam','age':16}
print(a) # I am adam  I 16 Twenty five years old 

5. sep sets the division symbol

The assignment of sep is a dividing symbol, which will be filled in the space inside the quotation marks between each element in turn


print('a','b','c','d',sep = ' = ' ) #a = b = c = d

6. flags Format Symbol

This is not very common

+, right alignment can set the total length of inserted characters, and the missing bits are not marked with spaces


a = ' I am %(name)+20s  I %(age)d Twenty five years old ' %{'name':'adam','age':16}
print(a) # I am                 adam  I 16 Twenty five years old 

Left alignment can be set to count the total length of the inserted characters, and the missing bits are not marked with spaces


a = ' I am %(name)-20s  I %(age)d Twenty five years old ' %{'name':'adam','age':16}
print(a) # I am adam                  I 16 Twenty five years old 

Space, right-aligned

0, right-aligned

3. format

1. Splicing is used by default

format also fills elements inside strings through placeholders

Use {} curly braces for placeholders. By default, elements are filled in the preceding curly braces in turn

If the number of elements filled in is insufficient, an error will be reported


a = ' I am {} , I {} Twenty five years old '.format('adam',16)
print(a) # I am adam , I 16 Twenty five years old 

2. Splice with indexes and slices

Use the index bits of elements to splice. You can take 1 element repeatedly but you can't exceed the index bits of Yuan Zu


a = ' I am {1} , I {0} Twenty five years old '.format('adam',16)
print(a) # I am 16 , I adam Twenty five years old 

Splicing by slices


b = 'adam'
a = ' I am %s  I %d Twenty five years old ' %(b,16)
print(a) # I am adam  I 16 Twenty five years old 
0

3. Call Yuanzu for splicing

Placement in the form of variable name


b = 'adam'
a = ' I am %s  I %d Twenty five years old ' %(b,16)
print(a) # I am adam  I 16 Twenty five years old 
1

4. Use a dictionary as the element library of format

Simply prefix the format element library with two asterisks


b = 'adam'
a = ' I am %s  I %d Twenty five years old ' %(b,16)
print(a) # I am adam  I 16 Twenty five years old 
2

5. Use lists as an element library for format

Just prefix the format element library with * two asterisks

It can also be combined with index splicing


a = ' I am {2} , I {1} I like to eat. I'm 20 years old {0}'.format(*['adam',16,' Braised '])
print(a) # I am braised, and I 16 I like to eat. I'm 20 years old adam

6. Placeholder operator

You can also put caret symbols such as placeholders in the position of placeholders

These symbols perform the corresponding transformation calculation on the inserted assignment

{: b} binary

{: o} Octal

{: d} Integer type

{: x} Hexadecimal and partially lowercase letters

{: X} Hexadecimal and partially uppercase letters

{:%} Display percentages default to 6 decimal places


b = 'adam'
a = ' I am %s  I %d Twenty five years old ' %(b,16)
print(a) # I am adam  I 16 Twenty five years old 
4

PS:

What # * and * * mean is

1, * Take out the elements in the list once in order

2. * * is to take out the key values of the dictionary in turn and change them into the form of variable assignment: (a= 'adam', b = 16, c= 'Braised')

PS

The output background font format and font background color can be set by the following command

From\ 033 [43; All characters between 1m and\ 033 [0m are added with background color, 43 controls the color of font background filling, font format, etc.


b = 'adam'
a = ' I am %s  I %d Twenty five years old ' %(b,16)
print(a) # I am adam  I 16 Twenty five years old 
5

Summarize

This article is here, I hope to give you help, but also hope that you can pay more attention to this site more content!


Related articles: