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 习题如下: 在自己本机执行如下程序,记录程序执行时间: # ...
随机推荐
- 解决scroll中addEventListener不生效的问题
这样写会出现一个情况,滚动的时候不打印任何值,并没有进入具体方法 但是下面这种方法 加一个true就会打印出相应的值
- Chrome浏览器崩溃
1.使用Win+R打开运行对话框,输入regedit,点击确定打开注册表: 2.找到"HKEY_LOCAL_MACHINE\SOFTWARE\Policies\Google\Chrome&q ...
- VUE学习-列表渲染
列表渲染 v-for <!-- v-for="(v,k,i) in arr/obj" --> <ul id="app"> <li ...
- oracle 行转列,动态年份,月份列。已解决!
-----------------存储过程包体----------- procedure GetComparativeAnalysisTB(p_StartTime varchar2, ----开始时间 ...
- 沁恒蓝牙系列芯片USB烧录故障排查
目录 使用USB烧录时让ISP工具能够识别芯片的操作是按住评估板上的"Download"按键,或者将PB22短接到GND,同时给板子上电.若是第一次拿到芯片,codeflash中是 ...
- bat 脚本用 winrar 自动打包压缩文件
bat 的文本如下: --声明winrar软件的位置set Path=;"C:\Program Files\WinRAR";%SystemRoot%\system32--用forf ...
- 1、docker 安装
1 准备三台linux服务器 2 docker安装 三台机器全部执行docker安装操作 2.1 移除以前docker相关包 sudo yum remove docker \ docker-clien ...
- shell语法4-printf命令、test命令和判断符号[]、逻辑运算符&&和||
一.printf命令 printf命令用于格式化输出,类似于C/C++中的printf函数. 默认不会在字符串末尾添加换行符!!! 例如: printf "%10d.\n" 123 ...
- 记录 springboot 整合swagger2 出现documentationPluginsBootstrapper&&NullPointerException异常
记录 由于springboot版本高 整合swagger2出现null异常 springboot 版本: swagger2 依赖版本: 出现的异常: 解决方案: application 添加如下依赖 ...
- 1.Python3.5+Pyqt5+PyCharm+Opencv3.3+Qtdesigner开发环境配置
一:Python3.3和Pyqt5的安装 注意:两个的版本一定要对应,一定要对应,一定要对应,重要的事情说三遍. 因为我自己的电脑是64位的,所以我下载的都是64位版本的,且都是3.5版本的:这两个一 ...