怎么注册个人网站,网页设计作业成品下载,营销网站试用,泸州市建设工程管理局网站String模块包含大量实用常量和类#xff0c;以及一些过时的遗留功能#xff0c;并还可用作字符串操作。1. 常用方法常用方法描述str.capitalize()把字符串的首字母大写str.center(width)将原字符串用空格填充成一个长度为width的字符串#xff0c;原字符串内容居中str.count…String模块包含大量实用常量和类以及一些过时的遗留功能并还可用作字符串操作。1. 常用方法常用方法描述str.capitalize()把字符串的首字母大写str.center(width)将原字符串用空格填充成一个长度为width的字符串原字符串内容居中str.count(s)返回字符串s在str中出现的次数str.decode(encoding’UTF-8’,errors’strict’)以指定编码格式解码字符串str.encode(encoding’UTF-8’,errors’strict’)以指定编码格式编码字符串str.endswith(s)判断字符串str是否以字符串s结尾str.find(s)返回字符串s在字符串str中的位置索引没有则返回-1str.index(s)和find()方法一样但是如果s不存在于str中则会抛出异常str.isalnum()如果str至少有一个字符并且都是字母或数字则返回True,否则返回Falsestr.isalpha()如果str至少有一个字符并且都是字母则返回True,否则返回Falsestr.isdigit()如果str只包含数字则返回 True 否则返回 Falsestr.islower()如果str存在区分大小写的字符并且都是小写则返回True 否则返回Falsestr.isspace()如果str中只包含空格则返回 True否则返回 Falsestr.istitle()如果str是标题化的(单词首字母大写)则返回True否则返回Falsestr.isupper()如果str存在区分大小写的字符并且都是大写则返回True 否则返回Falsestr.ljust(width)返回一个原字符串左对齐的并使用空格填充至长度width的新字符串str.lower()转换str中所有大写字符为小写str.lstrip()去掉str左边的不可见字符str.partition(s)用s将str切分成三个值str.replace(a, b)将字符串str中的a替换成bstr.rfind(s)类似于 find()函数不过是从右边开始查找str.rindex(s)类似于 index()不过是从右边开始str.rjust(width)返回一个原字符串右对齐的并使用空格填充至长度width的新字符串str.rpartition(s)类似于 partition()函数,不过是从右边开始查找str.rstrip()去掉str右边的不可见字符str.split(s)以s为分隔符切片strstr.splitlines()按照行分隔返回一个包含各行作为元素的列表str.startswith(s)检查字符串str是否是以s开头是则返回True否则返回Falsestr.strip()等于同时执行rstrip()和lstrip()str.title()返回”标题化”的str,所有单词都是以大写开始其余字母均为小写str.upper()返回str所有字符为大写的字符串str.zfill(width)返回长度为 width 的字符串原字符串str右对齐前面填充02.字符串常量常数含义string.ascii_lowercase小写字母’abcdefghijklmnopqrstuvwxyz’string.ascii_uppercase大写的字母’ABCDEFGHIJKLMNOPQRSTUVWXYZ’string.ascii_lettersascii_lowercase和ascii_uppercase常量的连接串string.digits数字0到9的字符串:’0123456789’string.hexdigits字符串’0123456789abcdefABCDEF’string.letters字符串’abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ’string.lowercase小写字母的字符串’abcdefghijklmnopqrstuvwxyz’string.octdigits字符串’01234567’string.punctuation所有标点字符string.printable可打印的字符的字符串。包含数字、字母、标点符号和空格string.uppercase大学字母的字符串’ABCDEFGHIJKLMNOPQRSTUVWXYZ’string.whitespace空白字符 ‘\t\n\x0b\x0c\r ‘3.字符串模板Template通过string.Template可以为Python定制字符串的替换标准,下面是具体列子from string import Templates Template($who like $what)print s.substitute(whoi, whatpython)i like pythonprint s.safe_substitute(whoi) # 缺少key时不会抛错i like $whatTemplate(${who}LikePython).substitute(whoI) # 在字符串内时使用{}ILikePythonTemplate还有更加高级的用法可以通过继承string.Template, 重写变量delimiter(定界符)和idpattern(替换格式), 定制不同形式的模板。import stringtemplate_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 replacedReplaced : %with_underscoreIngored : %notunderscoredDelimiter : $deReplaced : replacedIngored : %notunderscored原生的Template只会渲染界定符为$的情况重写后的MyTemplate会渲染界定符为%且替换格式带有下划线的情况。4.常用字符串技巧1.反转字符串 s 1234567890 print s[::-1]09876543212.关于字符串链接尽量使用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 sqlSELECT count() FROM table WHERE id 10 GROUP BY sex5.将print的字符串写到文件 print open(somefile.txt, w), Hello World # Hello World将写入文件somefile.txt转载于:https://my.oschina.net/jhao104/blog/829775