• 字符串格式化


    字符串格式化的两种方式:百分号格式、format格式

    1、百分号格式

    #打印字符串 
    msg="i am %s,my hobby is %s" %("alex","youxi")                           
    print(msg)                                                               
                                                                             
    #打印整数                                                                    
    msg1="name %s,age %d" %("land",28)                                       
    print(msg1)                                                              
                                                                             
    #打印浮点数                                                                   
    msg2="percent %f" %99.4566                                               
    print(msg2)                                                              
    msg3="percent %.2f" %234.5674                                            
    print(msg3)                                                              
                                                                             
    #打印百分比                                                                   
    msg3="percent %.2f %%" %234.5674                                         
    print(msg3)                                                              
                                                                             
    tlp="i am %(name)s,age %(age)d" %{"name":"land","age":12}                
    print(tlp)                                                               
                                                                             
    #字符串拼接                                                                   
    print("root","x","0","0",sep=":")                                        
                                                                             

    2、format格式

    tpl = "i am {}, age {}, {}".format("seven", 18, 'alex')
      
    tpl = "i am {}, age {}, {}".format(*["seven", 18, 'alex'])
      
    tpl = "i am {0}, age {1}, really {0}".format("seven", 18)
      
    tpl = "i am {0}, age {1}, really {0}".format(*["seven", 18])
      
    tpl = "i am {name}, age {age}, really {name}".format(name="seven", age=18)
      
    tpl = "i am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
      
    tpl = "i am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
      
    tpl = "i am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
      
    tpl = "i am {:s}, age {:d}".format(*["seven", 18])
      
    tpl = "i am {name:s}, age {age:d}".format(name="seven", age=18)
      
    tpl = "i am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
     
    tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
     
    tpl = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
     
    tpl = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
     
    tpl = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)
  • 相关阅读:
    PAT 05-树7 File Transfer
    PAT 05-树6 Path in a Heap
    PAT 10-2 删除字符串中的子串
    PAT 10-1 在字符串中查找指定字符
    PAT 10-0 说反话
    PAT 08-2 求矩阵的局部最大值
    PAT 07-3 求素数
    PAT 07-2 A+B和C
    PAT 07-0 写出这个数
    PAT 06-3 单词长度
  • 原文地址:https://www.cnblogs.com/Yangyl00/p/13149497.html
Copyright © 2020-2023  润新知