概述

在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. Git的使用(3) —— 远程版本库的操作(GitHub)

    1. 配置SSH (1) GitHub 登陆GitHub后,点击右上角头像,选择 Setting . 在左面栏目中选择"SSH and GPG keys". 打开生成的SSH公钥文 ...

  2. win10常用快捷键(热键)总结

    win10常用快捷键总结: win+Ctrl+D 创建虚拟桌面 win+Ctrl+左(右)键 切换虚拟桌面 win+Ctrl+F4 关闭虚拟桌面 win+D 隐藏所有窗口/显示所有窗口 win+M 隐 ...

  3. postgresql【二】postgresql强制删除数据库

    SELECT pg_terminate_backend(pg_stat_activity.pid) FROM pg_stat_activity WHERE datname='db_name' AND ...

  4. python 画园和椭圆

    from matplotlib.patches import Ellipse, Circle import matplotlib.pyplot as plt fig = plt.figure() ax ...

  5. spark sql插入表时的文件个数研究

    spark sql执行insert overwrite table时,写到新表或者新分区的文件个数,有可能是200个,也有可能是任意个,为什么会有这种差别? 首先看一下spark sql执行inser ...

  6. 异常检测-基于孤立森林算法Isolation-based Anomaly Detection-2-实现

    参考https://scikit-learn.org/stable/modules/generated/sklearn.ensemble.IsolationForest.html#sklearn.en ...

  7. o2s【我】

    //预处理对象转String方法 private String o2s(Object o) { if(o!=null&&!"".equals(o)&& ...

  8. mobile crane 1

    void MobileCrane::rotateRope2() { //double r_angle1 = rotateRope + rorate3; //std::cout << &qu ...

  9. 123457123457#0#----com.DoraGame.ShiZi75--前拼后广--识字dora

    com.DoraGame.ShiZi75--前拼后广--识字dora

  10. 常用快捷键 & BLOG & Website

    blog http://kejianttt.com/http://pptdesign.blogbus.com/ Website http://www.webdesignerdepot.com/   素 ...