str 方法总结整理

#!/usr/bin/env python
#Python 3 字符串常用方法
__author__ = "lrtao2010"

#capitalize 将字符串的首字符改为大写
# a = "lrtao"
# v = a.capitalize()
# print(v)
# Lrtao

#casefold 将字符串所有字符改为小写,功能强大
# a = "LrTao"
# v = a.casefold()
# print(v)
# lrtao

#lower 将字符串所有字符改为小写
# a = "LrTao"
# v = a.lower()
# print(v)
# lrtao

#upper 将字符全部转成大写
# a = "LrTao"
# v = a.upper()
# print(v)
# LRTAO

#center(self, width, fillchar=None) 内容居中,width:总长度(当width<len(s)时没有效果);
#                                   fillchar:空白处填充内容,默认无
# a = "lrtao"
# v = a.center(20,‘*‘)
# print(v)
# *******lrtao********

#ljust(self, width: int, fillchar: str = ...)内容左对齐,右侧填充,不指定默认空白
# a = "lrtao"
# v1 = a.ljust(20,‘*‘)
# v2 = a.ljust(20)
# print(v1)
# print(v2)
# lrtao***************
# lrtao

#rjust(self, width: int, fillchar: str = ...)内容右对齐,左侧填充,不指定默认空白
# a = "lrtao"
# v1 = a.rjust(20,‘*‘)
# v2 = a.rjust(20)
# print(v1)
# print(v2)
# ***************lrtao
#                lrtao

# zfill(self, width: int) z指zero,用0将字符填充到指定长度(左侧填充)
# a = "test"
# v = a.zfill(20)
# print(v)
# 0000000000000000test

# a = "test"
# v1 = a.rjust(20,‘0‘)
# print(v1)
# 0000000000000000test

#count(self, sub, start=None, end=None) 统计子序列在字符串里出现的次数,包括start,不包括end

# a = "lilelili"
# v1 = a.count(‘li‘)
# v2 = a.count(‘li‘,1)
# v3 = a.count(‘li‘,1,6)
# print(v1,v2,v3)
# 3 2 1
#encode(encoding=‘utf-8‘,errors=‘strict‘)  以encoding指定的编码格式对字符串进行编码
# a = "lilei"
# v = a.encode(encoding=‘utf-8‘)
# print(v)
# b‘lilei‘
#

#endswith(self, suffix, start=None, end=None)是否以指定字符串结束,返回值为布尔值

# a = "hello"
# v1 = a.endswith(‘o‘)
# v2 = a.endswith(‘l‘)
# v3 = a.endswith(‘lo‘)
# v4 = a.endswith(‘e‘,0,2)

# print(v1,v2,v3,v4)
# True False True True

#startswith(self, prefix: Union[str, Tuple[str, ...]], start: Optional[int] = ...,end: Optional[int] = ...)
#是否以指定字符串开始,返回值为布尔值
# a = "hello"
# v1 = a.startswith(‘h‘)
# v2 = a.startswith(‘e‘)
# v3 = a.startswith(‘he‘)
# v4 = a.startswith(‘e‘,1,3)
#
# print(v1,v2,v3,v4)
# True False True True

#expandtabs  把字符串的tab字符(\t)转化为空格,如果tab转换空格数量没有定义,默认一个tab转换成8个空格
# a = ‘I\tam\ttom‘
# v = a.expandtabs(4)
# print(v)
# I   am  tom

# find(self, sub, start=None, end=None) 返回首次找到子序列的位置,如果没找到,返回 -1
# a = ‘lilei‘
# v1 = a.find(‘l‘)
# v2 = a.find(‘l‘,2)
# v3 = a.find(‘lei‘,2,4)
# print(v1,v2,v3)
# 0 2 -1

#index(self, sub, start=None, end=None)返回首次找到子序列的位置,如果没找到程序会报异常
# a = ‘lilei‘
# v = a.index(‘ll‘)
# print(v)
# Traceback (most recent call last):
#     v = a.index(‘ll‘)
# ValueError: substring not found

#rfind(self, sub: str, __start: Optional[int] = ..., __end: Optional[int] = ...)
#返回指定子串的最大索引,如果没找到则返回-1,可以指定要开始和结束位置。
# a = ‘lilililili‘
# v1 = a.rfind(‘li‘)
# v2 = a.rfind(‘li‘,9)
# v3 = a.rfind(‘li‘,2,4)
# print(v1,v2,v3)
# 8 -1 2

#rindex(self, sub: str, __start: Optional[int] = ..., __end: Optional[int] = ...)
#返回指定子串的最大索引,如果没找到则抛异常,可以指定要开始和结束位置。
# a = ‘lilililili‘
# v1 = a.rindex(‘li‘)
# v2 = a.rindex(‘li‘,9)
# v3 = a.rindex(‘li‘,2,4)
# print(v1)
# print(v2)
# print(v3)
# v2 = a.rindex(‘li‘,9)
# ValueError: substring not found
#
# Process finished with exit code 1

#format 字符串格式化
# a = ‘hello ,{name}!hello ,{name2} !‘
# v = a.format(name=‘lilei‘,name2=‘tom‘)
# print(v)
# hello ,lilei!hello ,tom !

# a = ‘hello,{0}! hello,{1}!‘
# v = a.format(‘lilei‘,‘tom‘)
# print(v)
# hello,lilei! hello,tom!

#format_map(self, mapping) 和format相似,参数为字典形式
# a = ‘hello ,{name}!hello ,{name2} !‘
# v = a.format_map({‘name2‘:‘lilei‘,‘name‘:‘tom‘})
# print(v)
# hello ,tom!hello ,lilei !

#isalnum 是否只包含字母或数字,返回值为布尔值
# a = ‘lsL123‘
# b = ‘ls‘
# c = ‘LS‘
# d = ‘123‘
# e = ‘[email protected]‘
# v1 = a.isalnum()
# v2 = b.isalnum()
# v3 = c.isalnum()
# v4 = d.isalnum()
# v5 = e.isalnum()
# print(v1,v2,v3,v4,v5)
# True True True True False

#isalpha 字符串是否全部是字母
# a = ‘lsL123‘
# b = ‘ls‘
# c = ‘LS‘
# d = ‘Ls‘
# e = ‘[email protected]‘
# v1 = a.isalpha()
# v2 = b.isalpha()
# v3 = c.isalpha()
# v4 = d.isalpha()
# v5 = e.isalpha()
# print(v1,v2,v3,v4,v5)
# False True True True False

#isascii Return True if all characters in the string are ASCII, False otherwise.
# a = ‘12ls er$%^&*()‘
# b = ‘中‘
# v1 = a.isascii()
# v2 = b.isascii()
# print(v1,v2)
# True False

#isdecimal 字符串是否是十进制字符串
# a = ‘1234567890‘
# b = ‘A0‘
# v1 = a.isdecimal()
# v2 = b.isdecimal()
# print(v1,v2)
# True False

#isdigit 如果字符串是数字字符串,则返回True,否则返回False。
# isdecimal() 如果字符串是数字字符串,则返回True,否则返回False。
#isnumeric 如果字符串是数字字符串,则返回True,否则返回False

# num = "一"
# v1 = num.isdigit()
# v2 = num.isdecimal()
# v3 = num.isnumeric()
# print(v1,v2,v3)
# False False True

# num = "Ⅰ" #罗马数字1
# v1 = num.isdigit()
# v2 = num.isdecimal()
# v3 = num.isnumeric()
# print(v1,v2,v3)
# False False True

# num = "1"
# v1 = num.isdigit()
# v2 = num.isdecimal()
# v3 = num.isnumeric()
# print(v1,v2,v3)
# True True True

# isdigit()
# True: Unicode数字,byte数字(单字节),全角数字(双字节),罗马数字
# False: 汉字数字
# Error: 无
# isdecimal()
# True: Unicode数字,全角数字(双字节)
# False: 罗马数字,汉字数字
# Error: byte数字(单字节)
# isnumeric()
# True: Unicode数字,全角数字(双字节),罗马数字,汉字数字
# False: 无
# Error: byte数字(单字节)

#isidentifier 判断字符串是否包含python的保留字
# v1 = ‘def‘.isidentifier()
# v2 = ‘aaa‘.isdecimal()
# print(v1,v2)
# True False

#islower 字符串中不能出现大写字母,并且至少有一个小写字母
# a = ‘ls12#3‘
# b = ‘Ls‘
# c = ‘1234s‘
# d = ‘LS‘
# v1 = a.islower()
# v2 = b.islower()
# v3 = c.islower()
# v4 = d.islower()
# print(v1,v2,v3,v4)
# True False True False

#isprintable 判断字符串中所有的字符串都是可以通过repr表示成字符串,
#            或者字符串是空的,都返回True,否则返回False
# v = chr(33).isprintable()
# v1 = chr(1000000).isprintable()
# print(v,v1)
# True False

#isspace 判断字符串是否是空格
# a = ‘‘
# b = ‘ ‘
# v1 = a.isspace()
# v2 = b.isspace()
# print(v1,v2)
# False True

#title  标题格式(可以理解为单词首字母大写,其它字符小写)
# a = ‘this is title‘
# v = a.title()
# print(v)
# This Is Title

#istitle 判断是否是标题格式(可以理解为首字母是否大写)
# a = "this is title"
# b = "This is title"
# c = "This Is title"
# d = "This Is Title"
# v1 = a.istitle()
# v2 = b.istitle()
# v3 = c.istitle()
# v4 = d.istitle()
# print(v1,v2,v3,v4)
# False False False True

#isupper 判断字母是否全部是大写
# a = ‘lsL123‘
# b = ‘LSF123‘
# c = ‘LSF‘
# d = ‘LS F‘
# v1 = a.isupper()
# v2 = b.isupper()
# v3 = c.isupper()
# v4 = d.isupper()
# print(v1,v2,v3,v4)
# False True True True

#join(self, iterable: Iterable[str])返回一个用指定分隔符分隔的字,
#                                   或者是将指定字符加入到另一个字符中。
# a = ‘iamtom‘
# v1 = ‘.‘.join(a)
# v2 = ‘#‘.join(a)
# print(v1)
# print(v2)
# i.a.m.t.o.m
# i#a#m#t#o#m

#lstrip 移除左侧空白(没有测试成功)

#partition(self, sep: str) 按照指定的字符将字符串分为前中后三部分,从左侧开始
# a = "iamtom"
# v1 = a.partition("i")
# v2 = a.partition(‘am‘)
# v3 = a.partition(‘om‘)
# v4 = a.partition(‘som‘)
# print(v1)
# print(v2)
# print(v3)
# print(v4)
# (‘‘, ‘i‘, ‘amtom‘)
# (‘i‘, ‘am‘, ‘tom‘)
# (‘iamt‘, ‘om‘, ‘‘)
# (‘iamtom‘, ‘‘, ‘‘)

#rpartition(self, sep: str) 与partition一样,但是从右边开始
# a = "iamtom"
# v1 = a.rpartition("i")
# v2 = a.rpartition(‘am‘)
# v3 = a.rpartition(‘tom‘)
# v4 = a.rpartition(‘som‘)
# print(v1)
# print(v2)
# print(v3)
# print(v4)
# (‘‘, ‘i‘, ‘amtom‘)
# (‘i‘, ‘am‘, ‘tom‘)
# (‘iam‘, ‘tom‘, ‘‘)
# (‘‘, ‘‘, ‘iamtom‘)

#replace(self, old: str, new: str, count: int = ...) 替换字符,不指定次数默认全部替换
# a = ‘iamtomtomtom‘
# v1 = a.replace(‘m‘,‘i‘)
# v2 = a.replace(‘m‘,‘i‘,1)
# print(v1)
# print(v2)
# iaitoitoitoi
# iaitomtomtom

#split(self, sep: Optional[str] = ..., maxsplit: int = ...)
#按指定字符串从左侧开始进行切割,可以指定切割次数(不指定全部切割)

# a = ‘iamtomtom‘
# v1 = a.split(‘m‘)
# v2 = a.split(‘m‘,2)
# print(v1)
# print(v2)
# [‘ia‘, ‘to‘, ‘to‘, ‘‘]
# [‘ia‘, ‘to‘, ‘tom‘]

#rsplit(self, sep: Optional[str] = ..., maxsplit: int = ...)
#按指定字符串从右侧开始进行切割,可以指定切割次数(不指定全部切割)
# a = ‘iamtomtom‘
# v1 = a.rsplit(‘m‘)
# v2 = a.rsplit(‘m‘,2)
# print(v1)
# print(v2)
# [‘ia‘, ‘to‘, ‘to‘, ‘‘]
# [‘iamto‘, ‘to‘, ‘‘]

#strip(self, chars: Optional[str] = ...)
#移除两侧(最外侧)指定字符串,默认移除空格,以指定多个字符

# a = ‘ i am tom m‘
# v1 = a.strip()
# v2 = a.strip(‘m‘)
# v3 = a.strip(‘ap‘)
# v4 = a.strip(‘am‘)
# print(v1)
# print(v2)
# print(v3)
# print(v4)
# i am tom m
#  i am tom
#  i am tom m
#  i am tom

#rstrip(self, chars: Optional[str] = ...)
#移除右侧指定字符

# a = ‘ i am tom m‘
# v1 = a.rstrip()
# v2 = a.rstrip(‘m‘)
# v3 = a.rstrip(‘ap‘)
# v4 = a.rstrip(‘am‘)
# print(v1)
# print(v2)
# print(v3)
# print(v4)
 # i am tom m
 # i am tom
 # i am tom m
 # i am tom

 #splitlines(self, keepends: bool = ...)
 #按换行符\n切割,如果没指定keepends=True,则会将其从结果中移除
# a = "this is test1\n this is test2"
# v1 = a.splitlines(keepends=True)
# v2 = a.splitlines()
# print(v1)
# print(v2)
# [‘this is test1\n‘, ‘ this is test2‘]
# [‘this is test1‘, ‘ this is test2‘]

#swapcase 大小写转换
# a = ‘This Is Test 01‘
# v = a.swapcase()
# print(v)
# tHIS iS tEST 01

#translate(self, table: Optional[bytes], delete: bytes = ...)
#translate() 方法根据参数table给出的表(包含 256 个字符)转换字符串的字符,
# 要过滤掉的字符放到 deletechars 参数中

# intab = "aeiou"
# outtab = "12345"
# trantab = str.maketrans(intab, outtab)  # 制作翻译表
# str = "this is string example....wow!!!"
# print(str.translate(trantab))
# th3s 3s str3ng 2x1mpl2....w4w!!!

# 制作翻译表
# bytes_tabtrans = bytes.maketrans(b‘abcdefghijklmnopqrstuvwxyz‘, b‘ABCDEFGHIJKLMNOPQRSTUVWXYZ‘)
#
# # 转换为大写,并删除字母o
# print(b‘runoob‘.translate(bytes_tabtrans, b‘o‘))
# b‘RUNB‘

原文地址:https://www.cnblogs.com/lrtao2010/p/9276895.html

时间: 2024-10-15 10:41:14

str 方法总结整理的相关文章

str.方法的整理(字符串类型内置方法的具体使用)

1.str.strip() 方法:str.strip(self,chars) 作用:移除字符串头与尾的指定字符.(核心是头与尾) chars参数:需要移除的字符串,如果省略或者为None,则默认移除空格. 要点 ①:移除的字符串过程为从外向内,如果最外边没有要移除的字符串(假设里面却有的话),此时里面的那个字符串是不会被移除的. s1='/a=abcd*/-a' s2=s1.strip('a') print(s1) print(s2) 结果: /a=abcd*/-a /a=abcd*/- 需要移

编程中遇到的Python错误和解决方法汇总整理

这篇文章主要介绍了自己编程中遇到的Python错误和解决方法汇总整理,本文收集整理了较多的案例,需要的朋友可以参考下 开个贴,用于记录平时经常碰到的Python的错误同时对导致错误的原因进行分析,并持续更新,方便以后查询,学习.知识在于积累嘛!微笑+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++错误: 复制代码代码如下: >>> def f(x, y):      print x, y  >>> t

CentOS yum源设定使用方法的整理(转)

CentOS yum更新了很多版本更新,我本人认为CentOS yum很好使的文件系统,在此向大家推荐CentOS应该是做为服务器的linux的佼佼者.CentOS采用的二进制包是rpm,不过包的依赖性解决有时候却是个问题. 我比较喜欢debian的apt,非常方便.以前以为RedHat linux没这么方便,后来发现CentOS yum是个不错的东西.CentOS yum的使用和apt有几分相似,这样一来对于许多包的安装就方便多了. 这篇文章主要讲两点内容,一是CentOS yum使用方法的整

在phpmyadmin后台获取webshell方法汇总整理

方法一: CREATE TABLE `mysql`.`xiaoma` (`xiaoma1` TEXT NOT NULL ); INSERT INTO `mysql`.`xiaoma` (`xiaoma1` )VALUES ('<?php @eval($_POST[xiaoma])?>'); SELECT xiaomaFROM study INTO OUTFILE 'E:/wamp/www/7.php'; ----以上同时执行,在数据库: mysql 下创建一个表名为:xiaoma,字段为xia

关于python -os.mkdir(str)方法的使用记录

这几天在学习python,从昨天开始安装了ubuntu系统以后,就开始研究这个备份文件的例子,可是无论如何,总是不成功,不是说 OSError: [Errno 2] No such file or directory: 就是说 OSError: [Errno 13] Permission denied: 这些错误都是因为一个os.mkdir()的系统模块的方法,终于是把我惹急了,在这个方法之前添加了测试输出语句,完全可以执行,很明显就是这个方法搞得不对,google了一下os.mkdir();找

Python数据类型方法精心整理,不必死记硬背,看看源码一切都有了

Python认为一切皆为对象:比如我们初始化一个list时: li = list('abc') 实际上是实例化了内置模块builtins(python2中为__builtin__模块)中的list类: class list(object): def __init__(self, seq=()): # known special case of list.__init__ """ list() -> new empty list list(iterable) ->

Python str方法总结

1.返回第一个字母大写 S.capitalize(...) S.capitalize() -> string >>>a = 'shaw' >>> b = a.capitalize() >>> print b Shaw 2.按指定长度填充特定字符 center(...) S.center(width[, fillchar]) -> string >>> a = 'linux' >>> print a.cen

JS字符串方法总结整理

//javascript字符串方法总结 1.String.charAt(n)      //取得字符串中的第n个字符 2.String.charCodeAt(n)  //取得字符串中第n个字符的Unicode编码 3.String.concat(str1,str2,....)   //用于连接多个字符串,但是 + 更加简单易用 4.String.fromCharCode(Unicode_1,Unicode_2,.....)     //从Unicode字符编码来创建字符串,返回创建好的字符串.

js 面向对象方法初整理

<script> function f1(){ alert(1); } function f2(){ alert(2); } var func = ""; function f(id){ if(id%2 == 0){ func = f1;//将F1指针指向对应的函数 }else{ func = f2; } func();//调用相应的方法 } f(0); </script> <script> var anim = function(){} anim.