zoukankan      html  css  js  c++  java
  • Python常用方法

    list(set(X))

    format()

    Python2.6 开始,新增了一种格式化字符串的函数 str.format(),它增强了字符串格式化的功能。

    基本语法是通过 {} 和 : 来代替以前的 % 。

    format 函数可以接受不限个参数,位置可以不按顺序。

    "{} {}".format("hello", "world")    # 不设置指定位置,按默认顺序
    'hello world'
     
    "{0} {1}".format("hello", "world")  # 设置指定位置
    'hello world'
     
    "{1} {0} {1}".format("hello", "world")  # 设置指定位置
    'world hello world'

     也可以设置参数

    #!/usr/bin/python
    # -*- coding: UTF-8 -*-
     
    print("网站名:{name}, 地址 {url}".format(name="菜鸟教程", url="www.runoob.com"))
     
    # 通过字典设置参数
    site = {"name": "菜鸟教程", "url": "www.runoob.com"}
    print("网站名:{name}, 地址 {url}".format(**site))
     
    # 通过列表索引设置参数
    my_list = ['菜鸟教程', 'www.runoob.com']
    print("网站名:{0[0]}, 地址 {0[1]}".format(my_list))  # "0" 是必须的

    修改字体颜色/背景色

     导入termcolor模块

        print('Current terminal type: %s' % os.getenv('TERM'))
        print('Test basic colors:')
        cprint('Grey color', 'grey')
        cprint('Red color', 'red')
        cprint('Green color', 'green')
        cprint('Yellow color', 'yellow')
        cprint('Blue color', 'blue')
        cprint('Magenta color', 'magenta')
        cprint('Cyan color', 'cyan')
        cprint('White color', 'white')
        print(('-' * 78))
    
        print('Test highlights:')
        cprint('On grey color', on_color='on_grey')
        cprint('On red color', on_color='on_red')
        cprint('On green color', on_color='on_green')
        cprint('On yellow color', on_color='on_yellow')
        cprint('On blue color', on_color='on_blue')
        cprint('On magenta color', on_color='on_magenta')
        cprint('On cyan color', on_color='on_cyan')
        cprint('On white color', color='grey', on_color='on_white')
        print('-' * 78)
    
        print('Test attributes:')
        cprint('Bold grey color', 'grey', attrs=['bold'])
        cprint('Dark red color', 'red', attrs=['dark'])
        cprint('Underline green color', 'green', attrs=['underline'])
        cprint('Blink yellow color', 'yellow', attrs=['blink'])
        cprint('Reversed blue color', 'blue', attrs=['reverse'])
        cprint('Concealed Magenta color', 'magenta', attrs=['concealed'])
        cprint('Bold underline reverse cyan color', 'cyan',
                attrs=['bold', 'underline', 'reverse'])
        cprint('Dark blink concealed white color', 'white',
                attrs=['dark', 'blink', 'concealed'])
        print(('-' * 78))
    
        print('Test mixing:')
        cprint('Underline red on grey color', 'red', 'on_grey',
                ['underline'])
        cprint('Reversed green on red color', 'green', 'on_red', ['reverse'])

  • 相关阅读:
    mysql数据库开放远程连接的方法
    MySQL数据库字符集由utf8修改为utf8mb4一例
    解决silk-v3-decoder-master转换wav时,百度语音解析问题
    PHP高效率写法(详解原因)
    如何计算服务器能够承受多大的pv?
    微信开发中使用curl忽略https证书
    PHP libevent函数基本介绍
    PHP stream相关协议及上下文选项和参数归纳
    Centos搭建PHP5.3.8+Nginx1.0.9+Mysql5.5.17
    编码转换(UTF8->GBK)
  • 原文地址:https://www.cnblogs.com/jiaoyang77/p/8744337.html
Copyright © 2011-2022 走看看