概述

在Python3中,字符串格式化操作通过format()方法或者f’string’实现。而相比于老版的字符串格式化方式,format()方法拥有更多的功能,操作起来更加方便,可读性也更强。该函数将字符串当成一个模板,通过传入的参数进行格式化,并且使用大括号{}作为特殊字符代替%

位置设定

默认位置

不指定格式化位置,按照默认顺序格式化

S = 'I {} {}, and I\'am learning'.format('like', 'Python')
print(S)

示例结果:

I like Python, and I'am learning

设置位置

设置数字顺序指定格式化的位置

S = 'I {0} {1}, and I\'am learning'.format('like', 'Python')
print(S)
# 打乱顺序
S = 'I {1} {0} {1}, and I\'am learning'.format('like', 'Python')
print(S)

示例结果:

I like Python, and I'am learning
I Python like Python, and I'am learning

设置关键字

设置关键字指定格式化的内容叉车维修租赁

S = 'I {l} {p}, and I\'am learning'.format(p='Python', l='like')
print(S) S = 'I {p} {l}, and I\'am learning'.format(p='Python', l='like')
print(S)

示例结果:

I like Python, and I'am learning
I Python like, and I'am learning

参数传递

我们可以传入各种类型参数格式化字符串,即不限于字符串变量或数字等。

元组传参

利用元组传参,传参形式 *tuple

# 定义一个元组
T = 'like', 'Python'
# 不指定顺序
S = 'I {} {}, and I\'am learning'.format(*T)
print(S)
# 指定顺序
S = 'I {0} {1}, and I\'am learning'.format(*T)
print(S)

示例结果:

I like Python, and I'am learning
I like Python, and I'am learning

字典传参

# 定义一个字典
D = {'l':'like', 'p':'Python'}
# 指定键确定顺序
S = 'I {l} {p}, and I\'am learning'.format(**D)
print(S)

示例结果:

I like Python, and I'am learning

列表传参

# 定义一个列表
L0 = ['like', 'Python']
L1 = [' ', 'Lerning']
# `[]`前的0、1用于指定传入的列表顺序
S = 'I {0[0]} {1[1]}, and I\'am learning'.format(L0, L1)
print(S)

示例结果:

I like Lerning, and I'am learning

Python3之字符串格式化format函数详解(上)的更多相关文章

  1. Python3之字符串格式化format函数详解(下)

    格式限定符 format通过丰富的的“格式限定符”(语法是 {}中带:号)对需要格式的内容完成更加详细的制定. 进制转换 我们可以再限定符中制定不同的字符对数字进行进制转换的格式化,进制对应的表格: ...

  2. Python中格式化format()方法详解

    Python中格式化format()方法详解 Python中格式化输出字符串使用format()函数, 字符串即类, 可以使用方法; Python是完全面向对象的语言, 任何东西都是对象; 字符串的参 ...

  3. Python第二天 变量 运算符与表达式 input()与raw_input()区别 字符编码 python转义符 字符串格式化 format函数字符串格式化 帮助

    Python第二天  变量  运算符与表达式  input()与raw_input()区别  字符编码  python转义符  字符串格式化  format函数字符串格式化  帮助 目录 Pychar ...

  4. python3字符串格式化format()函数的简单用法

    format()函数 """ 测试 format()函数 """ def testFormat(): # format()函数中有几个元素, ...

  5. Python: 字符串格式化format()函数的使用

    python从2.6开始支持format,新的更加容易读懂的字符串格式化方法,从原来的% 模式变成新的可读性更强的 花括号声明{}.用于渲染前的参数引用声明, 花括号里可以用数字代表引用参数的序号, ...

  6. 我的第一个Python程序,定义主函数,eval、format函数详解,

    程序实例: #第一个py小程序 def main(): f = eval(input("输入一个数值:")) p=f*(5/9) print("现在的值为:{0:3.3f ...

  7. python基础(二)字符串內建函数详解

    字符串 定义:它是一个有序的字符的集合,用于存储和表示基本的文本信息,''或""或''' '''中间包含的内容称之为字符串特性:1.只能存放一个值2.不可变,只能重新赋值3.按照从 ...

  8. 转;VC++中Format函数详解

    Format是一个很常用,却又似乎很烦的方法,以下是它的完整概貌,以供大家查询之用: 一.字符串 首先看它的声明: function Format(const Format: string; cons ...

  9. python 字符串的split()函数详解

    split翻译为分裂.  split()就是将一个字符串分裂成多个字符串组成的列表. split()当不带参数时以空格进行分割,当代参数时,以该参数进行分割. //---当不带参数时 example: ...

随机推荐

  1. Sphinx全文索引引擎

    一.什么是sphinx 原理:sphinx将数据库中的表建立索引,php操作sphinx时,将要查询的关键字进行匹配,返回一个id,php通过id到数据库中查询数据. 二.下载 链接:https:// ...

  2. OpenCV reshape The Matrix is not continuous, thus its number of rows can not be changed

    When using OpenCV  reshape and gets this error: OpenCV Error: Image step is wrong (The matrix is not ...

  3. linux安装phantomjs,-bash: /usr/local/bin/phantomjs: is a directory解决方案

    首先安装依赖——fontconfig和freetypeyum install fontconfig freetype2在官网上下载对应版本的包http://phantomjs.org/download ...

  4. vue element-UI Form表单验证

    摘自官网 https://element.eleme.cn/#/zh-CN/component/form 保证prop的值等于v-model的值,并且初始化值,这样验证才好使. 可以自定义验证 < ...

  5. python判断字符串包含关系

    转自---http://blog.csdn.net/yl2isoft/article/details/52079960 1.使用成员操作符 in >>> s='nihao,shiji ...

  6. JS数组常见方法的深浅拷贝分类

    一.涉及浅拷贝类方法,会改变原数组 1,pop():   删除 arrayObject 的最后一个元素,把数组长度减 1,并且返回它删除的元素的值.如果数组已经为空,则 pop() 不 改变数组,并返 ...

  7. PHP如何实现静态的链式调用

    Db::table('**')->where('***','***')->order('***')->find('**'); 想这种应该怎么实现 public function ta ...

  8. OpenGL ES3使用MSAA(多重采样抗锯齿)的方法

    昨晚花费了我2个多小时的时间终于把OpenGL ES3.0中的MSAA给搞定了.在OpenGL ES2.0中,Khronos官方没有引入标准的MSAA全屏抗锯齿的方法,而Apple则采用了自己的GL_ ...

  9. Qt tableview加载数据

    Qt tableview加载数据 //把数据加载到tableView void ImportData::loadDataInTableView() { ) { if (pageNum>stude ...

  10. 生成有目录的pdf

    生成有目录的pdf 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 https://jingyan.baidu.com/article/ff411625c2153512e48237a ...