A guide to string formatting in Python

String formatting

There are usually three forms of controlling string format in Python:

  • % placeholder (formatter)
  • str.format() function
  • f-string embedded

Python first started formatting strings using %, but its fatal disadvantage is that the types it supports are limited. It only supports three types: int, str, and double, which results in that all other types can only be converted ( Force conversion) to these types, and if a tuple is passed, a single-value tuple must also be passed in. For this reason, str.format() has been added to solve some problems in %-formatting. In particular, it uses normal function call syntax (and therefore supports multiple arguments) and can be extended via the __format__() method on objects that are converted to strings. However, str.format() has the problem of code redundancy, and f-string provides a concise and easy-to-read way to include the value of Python expressions in strings, including lambda expressions (to be placed in parentheses) ).


% placeholder (formatter)

Format characters reserve space for actual values ​​and control the format of display

grammar:

  • Placeholder in string:%[flags][width][.precision]TypeCode
  • Parameters passed after the string: % formatted object

Parameter Description:

  • flags: can be + - ’ ’ or 0

    • +: Indicates right alignment
    • -: Indicates left alignment
    • ' ': is a space, which means padding a space to the left of the positive number to align it with the negative number.
    • 0: Indicates filling with 0
  • width: indicates the display width

  • precision: represents the precision after the decimal point

  • TypeCode: Type code, used to control the type of display

    • %s: Displayed in the form of a string (displayed using str())

    • %r: Displayed in the form of a string (displayed using repr())

    • %c: Displayed in the form of a single character (passing multiple characters will result in an error)

    • %b: Displayed as a binary integer

    • %d : Displayed as a decimal integer

    • %i: Displayed as a decimal integer

    • %o: Displayed as an octal integer

    • %x: Displayed as a hexadecimal integer

    • %f: Displayed in the form of a floating point number, retaining six significant digits after the decimal point

      %.2f: keep 2 decimal places

    • %F: Displayed in the form of floating point number, the same as above

    • %e: Displayed in the form of an exponent (the base is written as e), retaining six significant figures after the decimal point

      %.2e: Reserve 2 decimal places, use scientific notation

    • %E: Displayed in exponential form (the base is written as E)

    • %g: Displayed in the form of exponent (e) or floating point number

      According to the display length, decimal method is used on the premise of ensuring six significant digits, otherwise scientific notation is used.

    • %G: Displayed in the form of exponent (E) or floating point number (according to the display length)

    • placeholder width output

      • %20s: Right justified, placeholder 20 bits
      • %-20s: Left justified, placeholder 20 bits
      • %.3s: intercept 3 characters
      • %20.3s: 20 placeholders, intercept 3 characters
      • %-10s%10s: left 10 placeholders, right 10 placeholders

Example:

  • String output

    print('hi! %s!' %('Echohye'))  # 如果只有一个数,%后面可以不加括号,如print('hi! %s!'%'Echohye')
    # 输出:hi! Echohye!
    
    print('hi! %20s!' % 'Echohye')	# 占位符宽度输出
    # 输出:hi!              Echohye!
    
    name = 'Echohye'
    print('hi! %s' %(name))
    # 输出:hi! Echohye
    
    id = '123'
    print('%s的id是%s' %(name, id))
    # 输出:Echohye的id是123
    
  • integer output

    print('今年%d岁了' %(20))
    # 输出:今年20岁了
    
    print('%e'%1.11111)
    # 输出:1.111110e+00
    print('%.2e'%1.11111)
    # 输出:1.11e+00
    
    print('%g'%1.2345678)
    # 输出:1.23457
    print('%.2g'%1.2345678)
    # 输出:1.2
    
  • floating point output

    print('%f' %1.2345)
    # 输出:1.234500
    print('%.2f' %1.2345)
    # 输出:1.23
    

format() function

  • Starting with Python 2.6, a new function str.format() has been added to format strings, which enhances the function of string formatting.

  • The basic syntax is to replace the previous % with {} and :

  • The format() function can accept unlimited parameters, and the positions do not need to be in order.

    • Without numbering, i.e. "{}", in default order

    • With serial number placeholder, the order can be reversed, that is, "{1}", "{2}"

    • Placeholder with name, i.e. “{a}”, “{tom}”

      Note: Serial number placeholder and name placeholder can be mixed, but it is generally not recommended.

Example:

  • String output

    # 不设置指定位置,按默认顺序
    print("{} {}!".format("hello", "Echohye")) 
    # 输出:'hello Echohye!'
    
    # 设置指定位置,序号占位符
    print("{0} {1}!".format("hello", "Echohye")) 
    # 输出:'hello Echohye!'
    print("{1} {0}!".format("hello", "world")) 
    # 输出:'Echohye hello!'
    # 设置指定位置,名称占位符
    print("{b} {a}!".format(a="hello", b="world")) 
    # 输出:'Echohye hello!'
    
    # 通过字典设置参数
    site = {
          
          "name": "Echohye", "wxNum": "Echo_hyee"}
    print("名字:{name}, 微信号:{wxNum}".format(**site)) # **不能落下
     
    # 通过列表索引设置参数
    list = ['Echohye', 'Echo_hyee']
    print("名字:{0[0]}, wxNum:{0[1]}".format(list)) # “0”不能落下
    
    list = [['Echohye', 'Echo_hyee'],['小二', '12345']]
    print("名字:{0[0]}, wxNum:{0[1]}".format(list))
    # 输出:名字:['Echohye', 'Echo_hyee'], wxNum:['小二', '12345']
    print("名字:{0[1][0]}, wxNum:{0[1][1]}".format(list))
    # 输出:名字:小二, wxNum:12345
    print("名字:{0[0]}, wxNum:{0[1]}".format(list[0]))
    # 输出:名字:Echohye, wxNum:Echo_hyee
    
  • integer output

    print('{}'.format(1314))
    # 输出:1314
    print('{:.0f}'.format(1314.22)
    # 输出:1314
    
  • floating point output

    # 保留小数后两位
    print('{:.2f}'.format(3.1415926))
    # 输出:3.14
    
    # 带符号保留小数后两位,+ 表示在正数前显示 +,在负数前显示 -
    print('{:+.2f}'.format(3.1415926))
    # 输出:+3.14
    print('{:+.2f}'.format(-3.1415926))
    # 输出:-3.14
    
    # 不带小数
    print('{:.0f}'.format(3.1415926)) # “0”不能省
    # 输出:3
    
  • placeholder width output

    # 空格补x,填充左边,宽度为10
    # 说明:x只能是指定一个字符,不可以多位,可以是任何字符,默认为空格
    print('{:x>10s}'.format('happy'))
    # 输出:xxxxxhappy
    
    # 空格补x,填充右边,宽度为10
    print('{:x<10s}'.format('happy'))
    # 输出:happyxxxxx
    
    # 空格补x,填充两边,宽度为10
    print('{:x^10s}'.format('happy'))
    # 输出:xxhappyxxx 	# 如果占位符数是奇数,则右边多于左边
    
  • Other formats

    # 以逗号分隔的数字格式
    print('{:,}'.format(999999999))
    # 输出:999,999,999
    
    # 百分比格式
    print('{:%}'.format(0.99999)) # 默认仍是六位小数
    # 输出:99.999000%
    print('{:.2%}'.format(0.99999))	# 大于显示的位数则四舍五入
    # 输出:100.00%
    print('{:.2%}'.format(0.9999))
    # 输出:99.99%
    
    # 指数记法
    print('{:.0e}'.format(1000000))
    # 输出:1e+06
    print('{:.2e}'.format(1000000))
    # 输出:1.00e+06
    print('{:.2e}'.format(0.1000000))
    # 输出:1.00e-01
    print('{:.2e}'.format(0.000001))
    # 输出:1.00e-06
    

f-string embedded

f-string embedded: A string literal marked with 'f' or 'F' prefix, which can contain replacement fields, that is, {} marked expression

The f-string inline is evaluated at runtime and formatted using the format() function. The usage is similar to str.format(), but f-string is more concise, convenient and efficient.

Common uses:

  • Basic usage: f-string uses braces { } to represent the replaced field, and you can directly fill in the replacement content

    # 字符串输入
    print(f"my name is {
            
            'Echohye'}") # 这里得注意""和''的嵌套了
    # 参数代入
    name = 'Echohye'
    print(f'my name is {
            
            name}')
    # 输出:my name is Echohye
    
  • Expression evaluation and function calling:

    • The curly braces { } of f-string can be filled in with expressions or functions called, and Python will calculate the result and fill it in the returned string.

      print(f"They have {
                
                2+5*2} apples")		# 输出:They have 12 apples
      
      name = "Huang Wei"
      print(f"my name is {
                
                name.lower()}")		# 输出:my name is huang wei
      
    • Use lambda anonymous functions in f-string: you can do complex numerical calculations

      Note: Pay attention to the syntax format. The first parentheses represent the lambda expression, and the second parentheses represent the parameters passed to the lambda expression.

      aa = 123.456
      print(f"{
                
                (lambda x:x*5-2)(aa):.2f}")	# 输出:615.28
      
      bb = 8
      cc = 2
      print(f"{
                
                (lambda x,y:x+y)(bb,cc)}")		# 输出:10
      
  • Problems with using quotes in f-string

    • The quotation marks used within f-string braces cannot conflict with the quotation marks outside the braces (delimiter quotation marks). You need to flexibly switch between single quotation marks, double quotation marks, single triple quotation marks, and double triple quotation marks according to the situation.

      Note: As long as the quotes inside and outside the braces are different, there is no problem. However, only single quotes and double quotes can be used within the braces. The quotes outside the braces (delimiter quotes) can use single quotes, double quotes, single triple quotes, or double triple quotes.

    • Quotation marks inside delimiter quotes outside curly braces can also be escaped using the \ symbol, but they cannot be escaped using the \ symbol inside the curly braces.

      print(f"he\'ll go to {
                
                'shang hai'}")	# 输出:he'll go to shang hai
      
  • Problems with using braces in f-string

    When you want to print in f-string{}, you need to use double curly brackets { {}} , the content inside the double brackets will not be rendered

    print(f"5{
            
            '{apples}'}")		# 输出:5{apples}
    print(f"5{
           
           {'apples'}}")		# 输出:5{'apples'}
    print(f"{
           
           {5}}{
            
            'apples'}")	# 输出:{5}apples
    
  • time formatting

    Commonly used special format types: The format type given by the standard library datetime for formatting time information, applicable to date, datetime and time objects

    import datetime
    e = datetime.datetime.today()
    f'the time is {
            
            e:%Y-%m-%d %H:%M:%S}'	# 输出:the time is 2023-08-17 11:05:06
    
    format descriptor meaning Display sample
    %a Day of the week (abbreviation) ‘Sun’
    %A Day of the week (full name) ‘Sunday’
    %In Day of the week (number, 0 is Sunday, 6 is Saturday) ‘0’
    %in Day of the week (number, 1 is Monday, 7 is Sunday) ‘7’
    %d Day (number, padded with 0s to two digits) ‘07’
    %b month (abbreviation) ‘Aug’
    %B month (full name) ‘August’
    %m Month (number, two digits padded with 0) ‘08’
    %and Year (last two digits, padded with 0s) ‘14’
    %AND Year (complete number, no zero padding) ‘2014’
    %H Hour (24-hour format, two digits padded with 0s) ‘23’
    %I Hours (12-hour clock, zero-padded to two digits) ‘11’
    %p morning afternoon ‘PM’
    %M Minutes (two digits padded with 0s) ‘23’
    %S Seconds (two digits padded with 0s) ‘56’
    %f Microseconds (six digits padded with 0s) ‘553777’
    %With UTC offset (format is ±HHMM[SS], returns an empty string if no time zone is specified) ‘+1030’
    %WITH Time zone name (if no time zone is specified, an empty string is returned) 'EAST'
    %j Day of the year (complete with 0 to three digits) ‘195’
    %IN The week of the year (the week after the first Sunday of the year is week 0, supplemented by 0 to two digits) ‘27’
    %In The week of the year (the week after the first Monday of the year is week 0, supplemented by 0 to two digits) ‘28’
    %IN Week number of the year (week 1 is the first week of the year that contains January 4, supplemented by 0 to two digits) ‘28’

Description of legal formality

[[fill]align][sign][#][0][width][grouping_option][.precision][type]

# 选项:
fill (填充字符)            :<any character>
align (对齐方式)           :"<" ,">","=","^"
sign (数字标记)            :"+","-"," "
width (最小字段宽度)        :任意正数
grouping_option (数值分隔符号) 	:"_",","
precision (准确率)      	:任意正数
type (类型)           	 :"b","c","d","e","E","f","F", "g","G","n","o","s","x", "X","%"
  • fill Option: fill character

    align Option: Alignment

    • <: Left-justified (default string alignment)
    • >: Right-aligned (default alignment of values)
    • =: When filling, it is forced to fill between the positive and negative signs and numbers. Only padding of numbers is supported.
    • ^: means centered

    Note:

    • Unless a minimum field width is defined (width option), the field width will always be the same size as the data filling it, and the alignment options will be meaningless.

    • If the align value is specified, it can be preceded by a padding character that can be any character and defaults to a space.

      You cannot use literal braces ("{" or "}") as padding characters in a formatted string literal or using the str.format() method. However, it is possible to insert braces with nested replacement fields.

    Example:

    print(f'{
            
            "zhangsan":^18}')		# 输出:     zhangsan     
    print(f'{
            
            "zhangsan":a^18}')		# 输出:aaaaazhangsanaaaaa
    
  • sign Options: Descriptor. This option only works with numeric values

    • +: Forces the use of positive and negative signs for numbers
    • -: Use leading minus sign only for negative numbers (used by default)
    • Space: use a space as the leading number for positive numbers, and still use '-' as the leading number for negative numbers.

    Example:

    print(f'{
            
            199:+}')	# 输出:+199
    
  • #Options: Change the number display mode to control whether to display the base prefix. This option is only valid for integer (integer), float, and complex types.

    Alternative forms are defined differently for different types. :

    • For integers, this option adds the prefix "0b", "0o" or "0x" to the output value when using binary, octal or hexadecimal output.

    • For floating point numbers, complex numbers, and decimal, the replacement form causes the conversion result to always contain a decimal point character, even if there is no digit after it.

      Typically, decimal point characters appear in the results of these conversions only if they are followed by a number.

    • Additionally, for "g" and "G" conversions, trailing zeros are not removed from the result

    # b:二进制
    print(f"{
            
            10:b}")	# 输出:1010
    print(f"{
            
            10:#b}")	# 输出:0b1010
    
  • grouping_option Option: Separate thousandths of the integer part of the number

    Optional values:

    • , (comma): Use , as thousands separator

      Applies only to floating point numbers, complex numbers, and decimal integers. For floating point and complex numbers, only the digits before the decimal point are separated

    • _ (underscore): Use _ as thousands separator

      Applicable to floating point numbers, complex numbers and binary, octal, ten and hexadecimal integers:

      • For floating point and complex numbers, _ only separates the digits before the decimal point;
      • For binary, octal, and hexadecimal integers, one is inserted every four digits from low to high digits_ (for decimal integers, one _ is inserted every three digits) a>

    Example:

    money = 19999999877
    print(f'{
            
            money:,}') # 输出:19,999,999,877
    print(f'{
            
            money:_}') # 输出:19_999_999_877
    
  • width Option: Specifies the width of the minimum field, as a decimal integer. If not specified, field width is determined by content

  • precision selection: precision

    • For numeric objects, specifies the number of decimal places for the number, if there are decimals;
    • For non-numeric objects, it is used to specify the maximum length of the formatted characters finally returned. That is, after the formatting is completed, the result is intercepted with the precision parameter.

    For details on the use of precision, see:python - sklearn calculates precision (Precision)

    Floating point formatting example:

    # 前导0、统一宽度右对齐、千分位、小数点后固定位数、百分比
    a = 48424174
    print(f"{
            
            a:012.2f}:{
            
            a:12.3f}:{
            
            a:12,.2f}:{
            
            a:12.1%}")
    # 输出:000004842.42:    4842.417:    4,842.42:   484241.7%
    
  • type Options: Specify character: \A Returns a match if the specified character is at the beginning of the string

    Basic case type:

    format descriptor Meaning and function Applicable variable types
    s Normal string format string
    b Binary integer format integer
    c Character format, convert integers into corresponding characters according to unicode encoding integer
    d Decimal integer format integer
    O Octal integer format integer
    x Hexadecimal integer format (lowercase letters) integer
    X Hexadecimal integer format (uppercase letters) integer
    It is Scientific notation format, represented by e ×10^ Floating point numbers, complex numbers, integers (automatically converted to floating point numbers)
    AND Equivalent to e, but expressed as E ×10^ Floating point numbers, complex numbers, integers (automatically converted to floating point numbers)
    f Fixed point format, default precision is 6 Floating point numbers, complex numbers, integers (automatically converted to floating point numbers)
    F Equivalent to f, but replace nan and inf with NAN and INF Floating point numbers, complex numbers, integers (automatically converted to floating point numbers)
    g General format, use f for decimals and e for large numbers Floating point numbers, complex numbers, integers (automatically converted to floating point numbers)
    G Equivalent to G, but use F for decimals and E for large numbers. Floating point numbers, complex numbers, integers (automatically converted to floating point numbers)
    % Percent format, the number is automatically multiplied by 100 and then typed in the f format, with the % suffix added. Floating point number, integer (automatically converted to floating point number)

Guess you like

Origin blog.csdn.net/footless_bird/article/details/133982943