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 习题如下: 在自己本机执行如下程序,记录程序执行时间: # ...
随机推荐
- element的表格组件label宽度设置
- C# 报表接口样例,简单实用
//连接视图名称,视图在数据库写好<%@ WebHandler Language="C#" Class="GetwmsReport" %> usin ...
- TensorFlow中的Variable 变量
简单运用 这节课我们学习如何在 Tensorflow 中使用 Variable . 在 Tensorflow 中,定义了某字符串是变量,它才是变量,这一点是与 Python 所不同的. 定义语法: s ...
- PS技能之电子签名+修白牙齿
PS技能 NO.1 电子签名 有时候由于时空的限制,本人无法签字,那么电子签名就有了它的作用啦![注:谨慎使用] 亲试有效的教程,现在就是做笔记的时候啦! 教程链接如下: https://blog.c ...
- e-flow归档流程如何修改附件
OQ有时候要把CRB流程中已结束的单子替换一个附件,来应付客户的Audit.到后台CRB库中的admin for change,找到那张单子替换其中的值,变成在流转中的状态,就可以替换附件了. 其他流 ...
- MC 末影人
#include <iostream> #include "minecraft.h" using namespace std; int x=225,y=115,z=23 ...
- nginx客户端真实IP配置
https://www.cnblogs.com/kevingrace/p/8269955.html include mime.types; default_type application/octet ...
- ntp子母钟(gps子母钟系统)时钟系统在智能交通系统中的重要性
ntp子母钟(gps子母钟系统)时钟系统在智能交通系统中的重要性 ntp子母钟(gps子母钟系统)时钟系统在智能交通系统中的重要性 技术交流:岳峰 15901092122 http://www.bjh ...
- U盘插上电脑上后自动备份文件
缘由 一直有个不好的习惯,将所有文件都存在个人U盘,下班直接拔了就回家. 前久把U盘插入MacBook后,U盘就无法读数据了,搞了很久也没搞定. 当然了,里面的数据也无法使用了. 所以,现在的想法: ...
- python 通过win32com操作vcf到outlook中,同时解决乱码问题
之前用的黑莓手机,故障后换了iphone,后来还是想用上黑莓Q10.于是有了该文章. 问题: 如何将iphone上的通讯录导入黑莓? 网上回答1:通过icloud将iphone中的通讯录导入黑莓手机. ...