Article From:

Formatted output

1.Percentage replacement

2.formatReplacement Dictionaries




1.Some simple and basic output formatting methods and basic functions of function format functions are summarized.

String formatted code:


%%Percentage mark
%cCharacter and its ASCII code
%sCharacter string
%dSigned integers (decimal)
%uUnsigned integer (decimal)
%oUnsigned integers (octal)
%xUnsigned integers (sixteen)
%XUnsigned integers (sixteen – ary capitals)
%eFloating-point numbers (scientific counting method)
%EFloating-point numbers (scientific counting, E instead of E)
%fFloating-point numbers (with a decimal point sign)
%gFloating-point numbers (using%e or%f according to the size of the value)
%GFloating-point numbers (similar to%g)
%pPointer (the memory address of the value of sixteen print values)
%nThe number of storage output characters is placed in the next variable of the parameter list.
You can specify the alignment of the required length string:< (default) left alignment> right alignment^ middle alignment= (used only for numbers) to make up after a decimal point'''
print('3:\t|', format(3.1415926, '<10.2f'))
print('4:\t|{0:<10},{1:<15}'.format('wangyu', 3.1415926))
print('5:\t|User ID: {uid} Last seen: {last_login}'.format(uid='root', last_login='5 Mar 2008 07:20'))

'''The formatting indicator can contain a display type to control the format.For example, floating point numbers can be formatted into general format or expressed in terms of power.'b'- binary. Output the number with a base of 2.'c'- character. Converting integers to corresponding Unico before printingThe de string.'d'- decimal integers. Output the number with a base of 10.'o'- octal. Output the number with a base of 8.'x'- sixteen. The number is output based on 16, and more than 9 digits are written in lowercase letters.'e'- power symbol. Print numbers by scientific counting. The power is expressed in'e'.'g'- general format. Output the value in fixed-point format. When the value is very large, it is printed in a power form.'n'- numbers. When the value is an integer, it is the same as'd', and the value is floatingThe number of points is the same as'g'. The difference is that it inserts the numeric separator according to the locale.'%' - percentage. Multiply the value by 100 and print it in fixed-point ('f') format. There will be a percentage sign behind it.'''

print('13:\t|{0:n}'.format(3.75))  # Floating-point number
print('14:\t|{0:n}'.format(3))  # integer

# Input form of control format
a = int(input('a:'))
b = int(input('b:'))
print('16:\t|%*.*f' % (a, b, 1.1415926))

print('19:\t|{digit:*^ 10.5f}'.format(digit=1.0 / 3))

Classes and types can define a __format__ () method to control how to format themselves.It accepts a formatting indicator as a parameter:'''

def __format__(self, format_spec):
    if isinstance(format_spec, unicode):
        return unicode(str(self))
        return str(self)



#Using the str.format () function

#Using a '=' placeholder
print('I\'m {},{}'.format('Hongten','Welcome to my space!'))
print('#' * 40)

#You can also use a placeholder in the form of'{0}','{1}'
print('{0},I\'m {1},my E-mail is {2}'.format('Hello','Hongten',''))
#The location of the placeholder can be changed
print('{1},I\'m {0},my E-mail is {2}'.format('Hongten','Hello',''))
print('#' * 40)

#A placeholder in the form of'{name}'
print('Hi,{name},{message}'.format(name = 'Tom',message = 'How old are you?'))
print('#' * 40)

#Mixed use of'{0}','{name}'
print('{0},I\'m {1},{message}'.format('Hello','Hongten',message = 'This is a test message!'))
print('#' * 40)

#The following format control
import math
print('The value of PI is approximately {}.'.format(math.pi))
print('The value of PI is approximately {!r}.'.format(math.pi))
print('The value of PI is approximately {0:.3f}.'.format(math.pi))

table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 7678}
for name, phone in table.items():
    print('{0:10} ==> {1:10d}'.format(name, phone))

table = {'Sjoerd': 4127, 'Jack': 4098, 'Dcab': 8637678}
print('Jack: {0[Jack]:d}; Sjoerd: {0[Sjoerd]:d}; ''Dcab: {0[Dcab]:d}'.format(table))

 3.f”{}” Double quotes are all contents that need to be included. {} is the content of formatting replacement.

a ,b = "456" ,"123"
c = f'I\'m {a},{b}'


Link of this Article: Formatted output

Leave a Reply

Your email address will not be published. Required fields are marked *