一、字符串的format方法有几种指定参数的方式:
(1)按照位置传参(默认方式),传入的参数与{}一一对应
(2)关键字传参,关键字(keyword)传递是根据每个参数的名字传递参数。关键字并不用遵守位置的对应关系。
(3)位置传参与关键字传参混合使用。

二、详细描述字符串的format方法如何格式化字符串:

第一种方法:

s1 = 'Today is {},the temperature is {} degrees.'
print(s1.format('Saturday',24))

第二种方法:

s2 = 'Today is {day},the temperature is {degree} degrees.'
print(s2.format(degree = 30,day = 'Saturday'))

第三种方法:

s4 = Today is {week},{1},the {0} temperature is {degree} degree.'
print(s4.format('abds',1234,degree = 45,week = 'Sunday'))

拓展—如果是对象呢:

class Person:
def __init__(self):
self.age = 12
self.name - 'Bill'
person = Person() s5 = "My name is {p.name},my age is {p.age}."
print(s5.format(p = person))

问题:如果format的字符串中含有正则表达式时,比如str = """select  regexp_replace('test20230301','[0-9]{8}|[0-9]{6}','')  from ads_project_table_relation  where  ds = {0} """ 在使用format函数指定参数就会报错:IndexError: tuple  index  out  of  range 。

原因分析:正则表达式中匹配长度{8}  和 {6},在调用format方法时和需要传参的{0}冲突。

解决办法:将不是传参部分的{和},使用{{和}}进行替换。

即:str = """select  regexp_replace('test20230301','[0-9]{{8}}|[0-9]{{6}}','')  from ads_project_table_relation  where  ds = {0} """

Python-字符串format方法指定参数的更多相关文章

  1. #python str.format 方法被用于字符串的格式化输出。

    #python str.format 方法被用于字符串的格式化输出. #''.format() print('{0}+{1}={2}'.format(1,2,3)) #1+2=3 可见字符串中大括号内 ...

  2. Python里format()方法基本使用

    '''第一种:自然连接''' #format 连接字符串 str = '{}使用的python是{}版本'.format('我','3.6.5') print(str) #打印结果:我使用的pytho ...

  3. 7. python 字符串格式化方法(2)

    7. python 字符串格式化方法(2) 紧接着上一章节,这一章节我们聊聊怎样添加具体格式化 就是指定替换字段的大小.对齐方式和特定的类型编码,结构如下: {fieldname!conversion ...

  4. 字符串format()方法的基本使用

    <模板字符串>.format(<逗号分隔的参数>) 其中,模板字符串是一个由字符串和槽组成的字符串,用来控制字符串和变量的显示效果.槽用大括号({})表示,对应format() ...

  5. 7. python 字符串格式化方法(1)

    7. python 字符串格式化方法(1) 承接上一章节,我们这一节来说说字符串格式化的另一种方法,就是调用format() >>> template='{0},{1} and {2 ...

  6. python字符串replace()方法

    python字符串replace()方法 >>> help(str.replace)Help on method_descriptor:replace(...)    S.repla ...

  7. python字符串的方法

    python字符串的方法 ############7个基本方法############ 1:join def join(self, ab=None, pq=None, rs=None): # real ...

  8. python字符串格式化方法 format函数的使用

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

  9. python中format()方法格式化字符串

    format()是python2.6新增的一个格式化字符串的方法,功能非常强大,有可能在未来完全替代%格式化方法,相比 % ,format()的优点有: 1 .格式化时不用关心数据类型的问题,form ...

  10. Python字符串解析方法汇总

    Python字符串方法解析 1.capitalize 将首字母大写,其余的变成小写 print('text'.capitalize()) print('tExt'.capitalize()) 结果: ...

随机推荐

  1. P1802-DP【橙】

    1.又是一道因为写了异常剪枝而调了好久的题,以后再也不写异常剪枝了,异常情况压根不该出现,所以针对出现的异常情况进行补救的异常剪枝是一种很容易出错的行为,做为两手准备也就罢了,但第一次写成的代码必须能 ...

  2. 【java】 向上转型的运用

    应用 :求面积 1,抽象类  Geometry . public abstract class Geometry { public abstract double getArea(); } 2,矩形 ...

  3. Python Code_03数据类型

    数据类型 author : 写bug的盼盼 development time : 2021/8/27 19:59 变量定义 name = '阿哈' print(name) print('标识',id( ...

  4. linux-目录树

  5. Intel 移动CPU天梯榜

    Intel酷睿i9-13980HX 2023 2121 Intel酷睿i9-13900HX 2023 2051 Intel酷睿i9-13950HX 2023 2005 4 + Intel酷睿i9-12 ...

  6. [转帖]金仓数据库KingbaseES分区表 -- 声明式创建分区表

    https://www.modb.pro/db/638045 1. 创建分区表同时创建分区 1.1 准备环境 # 创建分区表同时创建分区 create table tb1(id bigint,stat ...

  7. 【转帖】再谈TCP/IP三步握手&四步挥手原理及衍生问题—长文解剖IP

    https://www.zhoulujun.cn/html/theory/ComputerScienceTechnology/network/2015_0708_65.html 长文是对TCP IP的 ...

  8. [转帖]淫技巧 | 如何查看已连接的wifi密码

    https://blog.csdn.net/DynmicResource/article/details/120134984?spm=1001.2014.3001.5502 主题使用方法:https: ...

  9. [转帖]从v8到v9,Arm服务器发展之路

    https://zhuanlan.zhihu.com/p/615344155   01 ARM:3A大作 将 CPU 的设计与制造相分离的代工模式,给 AMD 提供了高度的灵活性.第二.三代 EPYC ...

  10. [转帖]Linux-文本处理三剑客grep详解

    https://developer.aliyun.com/article/885611?spm=a2c6h.24874632.expert-profile.311.7c46cfe9h5DxWK 简介: ...