string 字符串模块操作
1. 常用方法

2.字符串常量

3.字符串模板Template
通过string.Template可以为Python定制字符串的替换标准,下面是具体列子:
>>>from string import Template
>>>s = Template('$who like $what')
>>>print s.substitute(who='i', what='python')
i like python
>>>print s.safe_substitute(who='i') # 缺少key时不会抛错
i like $what
>>>Template('${who}LikePython').substitute(who='I') # 在字符串内时使用{}
'ILikePython'
Template还有更加高级的用法,可以通过继承string.Template, 重写变量delimiter(定界符)和idpattern(替换格式), 定制不同形式的模板。
import string
template_text = ''' Delimiter : $de Replaced : %with_underscore Ingored : %notunderscored '''
d = {'de': 'not replaced',
'with_underscore': 'replaced',
'notunderscored': 'not replaced'}
class MyTemplate(string.Template):
# 重写模板 定界符(delimiter)为"%", 替换模式(idpattern)必须包含下划线(_)
delimiter = '%'
idpattern = '[a-z]+_[a-z]+'
print string.Template(template_text).safe_substitute(d) # 采用原来的Template渲染
print MyTemplate(template_text).safe_substitute(d) # 使用重写后的MyTemplate渲染
输出:
Delimiter : not replaced
Replaced : %with_underscore
Ingored : %notunderscored
Delimiter : $de
Replaced : replaced
Ingored : %notunderscored
原生的Template只会渲染界定符为$的情况,重写后的MyTemplate会渲染界定符为%且替换格式带有下划线的情况。
4.常用字符串技巧
1.反转字符串
>>> s = '1234567890'
>>> print s[::-1]
0987654321
2.关于字符串链接
尽量使用join()链接字符串,因为’+’号连接n个字符串需要申请n-1次内存,使用join()需要申请1次内存。
3.固定长度分割字符串
>>> import re
>>> s = '1234567890'
>>> re.findall(r'.{1,3}', s) # 已三个长度分割字符串
['123', '456', '789', '0']
4.使用()括号生成字符串
sql = ('SELECT count() FROM table '
'WHERE id = "10" '
'GROUP BY sex')
print sql
SELECT count() FROM table WHERE id = "10" GROUP BY sex
————————————————
版权声明:本文为CSDN博主「yolosliu」的原创文章,遵循CC 4.0 by-sa版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/github_36601823/article/details/77815013
string 字符串模块操作的更多相关文章
- String字符串相关操作
.length 字符串长度.equals 比较字符串.equalIgnoreCase 比较字符串不区别大小写.charAt 获取字符串指定下标位置的字符.contains 判断字符串内是否包含某字符串 ...
- 4.String字符串类型操作
String类型操作 1.set key value 设置key对应的值为string类型的value 2.mset key1 value1 … keyN valueN 一次设置多个key的值 3. ...
- String字符串的操作
字符串的常用操作 # Author:nadech name = "my name is nadech" print(name.count("a")) print ...
- string字符串js操作
String 对象方法 方法 描述 anchor() 创建 HTML 锚. big() 用大号字体显示字符串. blink() 显示闪动字符串. bold() 使用粗体显示字符串. charAt() ...
- String - 字符串分割操作
如果我想将一个字符串按照每8位一组分为若干个块,然后存储在一个byte[ ]数组中,我首先需要确定这个byte数组的长度,但由于我无法确定这个字符串的长度是否可以被8整除,所以无法直接判断,因此需要对 ...
- string 字符串的操作 大全类的使用
Array.Sort(vv, string.CompareOrdinal); //ASCII排序 string[] words = { "The", "1quick&qu ...
- 【超值分享】为何写服务器程序需要自己管理内存,从改造std::string字符串操作说起。。。
服务器程序为何要进行内存管理,管中窥豹,让我们从string字符串的操作说起...... new/delete是用于c++中的动态内存管理函数,而malloc/free在c++和c中都可以使用,本质上 ...
- string字符串转数组
/** * THis_is_a_cat * This Is A Cat * * Cat A Is This * @author Administrator * */ public class Test ...
- python学习之-- redis模块操作 string
redis 模块操作之--> String String:redis中的string在内存中按照一个key 对应一个 value来存储. 使用方法如下:set(name, value, ex=N ...
- C风格字符串和C++ string 对象赋值操作的性能比较
<<C++ Primer>> 第四版 Exercise Section 4.3.1 部分Exercise 4.2.9 习题如下: 在自己本机执行如下程序,记录程序执行时间: # ...
随机推荐
- cuda+pytorch环境安装
本机cuda版本为v11.5 conda install cudatoolkit 使用 CUDA 11.3版本的配置 conda install pytorch==1.11.0 torchvision ...
- web3 的身份验证之以太坊签名消息
https://zhuanlan.zhihu.com/p/535573066 python 实现 #coding=utf-8 from web3 import Web3 from eth_accoun ...
- .net core使用 ELK
一 Linux 下安装部署 第一种方法:docker-compose 安装方式 1.1 创建 docker-compose.yml 文件 version: '3.1' services: elasti ...
- javascript的属性描述符
什么是属性描述对象(attributes object)? 顾名思义,就是用来描述对象属性的对象.javascript内部提供了一个数据结构,用来描述对象的属性以及控制属性的行为. 比如该对象的某属性 ...
- Java--Comparable接口实现,控制数组和列表的排序
实现Comparable 接口,可以获得的排序方法有 列表排序 Collections.sort(); 数组排序 Arrays.sort(); sort()方法中的参数是可以获取排序索引的对象或者按照 ...
- vue页面添加锚点后 点击不改变URL
html: <a @click="changeHash('#row')"> {{ $t("msg.desc1") }} </a> j ...
- python读取图片相关属性
背景:工作中用到一些基础的图片处理的任务,比如获取图片宽高.获取图片的旋转角度等等图片属性,都是比较零散的,这里简单做个记录备忘 这里用到的库exifread,安装 pip isntall exifr ...
- Docker部署Springboot+Vue项目
1 docker使用nginx部署vue项目 1.1 打包vue项目 npm run build vue项目路径下会增加一个dist文件夹,里面就是网页文件 1.2 使用docker 拉取nginx ...
- mysql 创建函数失败解决办法,版本 8.0.26
报错信息:[Err] 1418 - This function has none of DETERMINISTIC, NO SQL, or READS SQL DATA in its declarat ...
- 关于idea2021.1版本的左边不显示bpmn Editor的问题
我这里是自定义主题风格出现的问题 idea左上角File→选择setting→找到Color Scheme菜单,将主题颜色改为IntelliJ Light.在重启idea就好