• 技术文章 >Python技术 >Python基础教程

    Python format函数的用法

    小妮浅浅小妮浅浅2021-03-12 19:06:33原创16521

    在字符串的时候上,有时候会涉及到格式化的问题。在python的新版本更新中,我们得到了一个新的函数方法format,同时还可以与其他的函数结合使用。下面我们就format函数进行简单说明,然后带来使用方法的介绍。

    1、说明

    format是python2.6新增的一个格式化字符串的方法,format函数常与print()函数结合使用,具备很强的格式化输出能力。

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

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

    3、使用字典传入,在字典前加入**

    grade = {'I' : '拦路雨', '状态': '写博客'}
    print('{I}比较无聊,在{状态}'.format(**grade))#字典前加上**

    以上就是Python format函数的用法,能够对字符串进行格式化并输出,大家在学习完基本内容后,赶快试试吧。更多Python学习推荐:python教学

    专题推荐:python format
    品易云
    上一篇:Python pass函数是什么 下一篇:Python float函数怎么用

    相关文章推荐

    • python for循环是什么• python time模块是什么• python index函数是什么• python max函数怎么用• python list函数是什么• python input函数是什么• python find函数的用法• Python property()函数的用法• Python pass函数是什么

    全部评论我要评论

    © 2021 Python学习网 苏ICP备2021003149号-1

  • 取消发布评论
  • 

    Python学习网