• Python字符串格式化


    1、常用百分号格式化方式:

    STRING = "I am %s" %"alex"
    
    STRING = "I am %s age %d" %("alex", 18)
    
    STRING = "I am %(name)s age %(age)d" %({"name": "alex", "age": 18})
    
    STRING = "percent %.2f" %99.97623
    
    STRING = "I am %(pp).2f" %{"pp": 123.425556}
    
    STRING = "I am %(pp).2f %%" %{"pp": 123.425556}

    2、常用format格式化方式:

    STRING = "I am {}, age {}, {}".format("seven", 18, 'alex')
    
    STRING = "I am {}, age {}, {}".format(*["seven", 18, 'alex'])
    
    STRING = "I am {0}, age {1}, really {0}".format("seven", 18)
    
    STRING = "I am {0}, age {1}, really {0}".format(*["seven", 18]) 
    
    STRING = "I am {name}, age {age}, really {name}".format(name="seven", age=18)
    
    STRING = "I am {name}, age {age}, really {name}".format(**{"name": "seven", "age": 18})
    
    STRING = "I am {0[0]}, age {0[1]}, really {0[2]}".format([1, 2, 3], [11, 22, 33])
      
    STRING = "I am {:s}, age {:d}, money {:f}".format("seven", 18, 88888.1)
      
    STRING = "I am {:s}, age {:d}".format(*["seven", 18])
      
    STRING = "I am {name:s}, age {age:d}".format(name="seven", age=18)
      
    STRING = "I am {name:s}, age {age:d}".format(**{"name": "seven", "age": 18})
     
    STRING = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
     
    STRING = "numbers: {:b},{:o},{:d},{:x},{:X}, {:%}".format(15, 15, 15, 15, 15, 15.87623, 2)
     
    STRING = "numbers: {0:b},{0:o},{0:d},{0:x},{0:X}, {0:%}".format(15)
     
    STRING = "numbers: {num:b},{num:o},{num:d},{num:x},{num:X}, {num:%}".format(num=15)

    3、python脚本指定编码的两种方式

    #!/usr/bin/python
    # coding=utf-8
    
    #!/usr/bin/python
    # -*- coding: utf-8 -*-

    参考链接:
              https://www.cnblogs.com/czqq/p/6108557.html
              https://www.cnblogs.com/exusll/p/6393621.html?utm_source=tuicool&utm_medium=referral      #字符串操作
              https://www.crifan.com/python_head_meaning_for_usr_bin_python_coding_utf-8/

  • 相关阅读:
    配置sonar、jenkins进行持续审查
    maven命令解释
    Maven中-DskipTests和-Dmaven.test.skip=true的区别
    maven之一:maven安装和eclipse集成
    Eclipse安装Maven插件
    IntelliJ IDEA单元测试和代码覆盖率图解
    关于Spring中的<context:annotation-config/>配置
    Java开发之@PostConstruct和@PreConstruct注解
    Java定时任务的三种实现方法
    JAVA之Mybatis基础入门二 -- 新增、更新、删除
  • 原文地址:https://www.cnblogs.com/xwupiaomiao/p/10443689.html
Copyright © 2020-2023  润新知