Python action string (2)

Source: Internet
Author: User
Tags lua mongodump

Common operations for strings include, but are not limited to, the following operations:

字符串的替换、删除、截取、复制、连接、比较、查找、分割等

This will summarize the built-in operation methods of the string, with emphasis on showing it in the form of examples.

使用type获取创建对象的类 type(name)使用dir获取类的成员dir(name)使用vars获取类的成员和各个成员的值

Capitalize

    功能:字符串首字母大写    ‘swhthaitun‘    name.capitalize()    返回结果:‘Swhthaitun‘

Casefold () First letter lowercase

    name = ‘HelloWord‘    reault = name.casefold()    print(reault)    返回结果:helloword

Casefold

    功能:将字符串中所有的大写字母转换成小写字母    "[‘bsondump‘, ‘mongo‘, ‘mongod‘, ‘mongodump‘, ‘mongoexport‘, ‘mongofiles‘, ‘mongoimport‘, ‘mongooplog‘, ‘mongoperf‘, ‘mongoLLKJKKore‘, ‘mongos‘, ‘UUUngostat‘, ‘monGGtop‘]"    s1.casefold()    返回结果:"[‘bsondump‘, ‘mongo‘, ‘mongod‘, ‘mongodump‘, ‘mongoexport‘, ‘mongofiles‘, ‘mongoimport‘, ‘mongooplog‘, ‘mongoperf‘, ‘mongollkjkkore‘, ‘mongos‘, ‘uuungostat‘, ‘monggtop‘]"

Center

    功能:字符串宽度填充,使用原有字符串+填充字符构成指定长度的新的字符串    ‘swhthaitun‘    name.center(15)    返回结果:‘   swhthaitun  ‘ #默认以空格进行填充    name.center(16,‘*‘) 返回结果:‘***swhthaitun***‘ 功能:字符串居中,以‘*’分割(20为新产生字符串的总的宽度) name = ‘HelloWord‘ reault = name.center(20,‘*‘) print(reault) 返回结果:*****HelloWord******

Count

Function: Count the number of occurrences of a character in a string, or complete the above operation within a specified range of stringsName =' Swhthaitun 'Name.Count' h ') returns the result:2Name.Count' H ',0,3) #从索引值0-3 range of characters statistics  ' h ' occurrences return result: 1 Function: Count the number of occurrences of a subsequence name =  ' helloword ' Reault = name. count ( ' W ') #如果换成 0,python is case sensitive print (reault) return result: 1 Span class= "Hljs-keyword" >name =  ' helloword ' Reault = name.< Span class= "hljs-built_in" >count ( ' l ', 0, 3) #统计单个字符出现的次数, you can specify the starting range, and in Python the starting range of Gu Tou disregard the principle of the tail, that is [0,3) print (reault)       

Encode

    功能:对字符串进行编码操作    ‘swhthaitun‘    name.encode()    返回结果:b‘swhthaitun‘    功能:转变字符串的编码    name = ‘南非波波‘    reault = name.encode(‘gbk‘)    print(reault)    返回结果:b‘\xc4\xcf\xb7\xc7\xb2\xa8\xb2\xa8‘

EndsWith

    功能:判断字符串是否以某个字符串结尾的,返回值为bool型    ‘swhthaitun‘    name.endswith(‘s‘)    返回结果:False    name.endswith(‘n‘)    返回结果:True name.endswith(‘tun‘) 返回结果:True name = ‘Iamalatterboy‘ reault = name.endswith(‘y‘) print(reault) 返回结果:True 

Expandtabs

    功能:将制表符‘\t‘转换成指定宽度的tab键分割,默认tabsize=8    li = ‘sw\tht‘    li.expandtabs(4)    返回结果:‘sw ht‘ li.expandtabs() 返回结果:‘sw ht‘

Find

    功能:在字符串中查找指定字符串,找不到时返回-1    name = ‘swht‘    name.find(‘s‘)    返回结果:0 name.find(‘h‘) 返回结果:2

Format

    功能:格式化输出字符串    ‘I\‘m {},{}‘ #两个‘{}‘是占位符    li.format(‘swht‘,‘欢迎来中国‘)    返回结果:"I‘m swht,欢迎来中国" 参考:http://blog.chinaunix.net/uid-23802873-id-4477364.html 

__contains__

    功能:包含 -->‘eal‘ in name    name = ‘swhtkkskjj‘ reault = name.__contains__(‘swht‘) print(reault) 返回结果:True

Index

    功能:在字符串中查找指定的字符串,找不到时直接报错    ‘swhthaitun‘    name.index(‘w‘)    返回结果:1  

Join ()

    功能:字符串连接    ‘swhthaitun‘    ‘*‘.join(name)    返回结果:‘s*w*h*t*h*a*i*t*u*n‘

Isalnum

    功能:检查判断字符串是否包含字母数字字符(http://www.yiibai.com/python/string_isalnum.html)    name = ‘swhthaitun‘    name.isalnum()    返回结果:True

Isalpha

    功能:检测字符串是否只由字母组成(http://www.runoob.com/python/att-string-isalpha.html)    name = ‘swhthaitun‘    name.isalpha()    返回结果:True

Isdecimal

    功能:检查字符串是否只包含十进制字符。这种方法只存在于unicode对象。(参考:http://www.runoob.com/python/att-string-isdecimal.html)    name = ‘swhthaitun‘    name.isdecimal()    返回结果:False

IsDigit

    功能:检测字符串是否只由数字组成。(参考:http://www.runoob.com/python/att-string-isdigit.html)    name = ‘swhthaitun‘    name.isdigit()    返回结果:False

Isidentifier

    功能:检测字符串是否是字母开头    ‘swhthaitun‘    name.isidentifier()    返回结果:True    name = ‘1swhthaitun‘    name.isidentifier()    返回结果:False

IsNumeric

    功能:检测字符串是否只由数字组成。这种方法是只针对unicode对象。    name = ‘swhthaitun‘    name.isnumeric()    返回结果:False    Li = ‘5523‘    Li.isnumeric()    返回结果:True

Isprintable

    功能:判断字符串中所有字符是否都属于可见字符    "\tPuppy"    a.isprintable()    返回结果:False    name = ‘swhthaitun‘    name.isprintable()    返回结果:True

Isspace

    功能:检测字符串是否为空格    ‘swhthaitun‘    name.isspace()    返回结果:False    Li = ‘ ‘    Li.isspace()    返回结果:True

Istitle

    功能:判断字符串是否适合当作标题(其实就是每个单词首字母大写)    "a puppy"    b = "Puppy"    a.istitle()    返回结果:False    b.istitle()    返回结果:True

Isupper

    功能:判断字符串中所有字母字符是否都是大写字母    "puppy"    b = "PUPPY"    a.isupper()    返回结果:False    b.isupper()    返回结果:True

Ljust

    功能:返回一个原字符串左对齐,并使用空格填充至指定长度的新字符串。如果指定的长度小于原字符串的长度则返回原字符串。(参考:http://www.runoob.com/python/att-string-ljust.html)    语法:str.ljust(width[, fillchar])         width -- 指定字符串长度。         fillchar -- 填充字符,默认为空格。    name = ‘swhthaitun‘    name.ljust(50,‘*‘) 返回结果:‘swhthaitun****************************************‘

Lower

    功能:将所有的字母转换成小写字母    ‘SWHT‘    name.lower()    返回结果:‘swht‘

Lstrip

    功能:去除字符串左边开头的空格    ‘  swht   ‘    name.lstrip()    返回结果:‘swht   ‘

Rstrip

    功能:去除字符串右边结尾的空格    ‘  swht   ‘    name.rstrip()    返回结果:‘   swht‘

Strip

    功能:去除字符串两边的空格    ‘  swht   ‘    name.rstrip()    返回结果:‘swht‘

Maketrans

    功能:用于创建字符映射的转换表,对于接受两个参数的最简单的调用方式,第一个参数是字符串,表示需要转换的字符,第二个参数也是字符串表示转换的目标。    注:两个字符串的长度必须相同,为一一对应的关系。    语法:str.maketrans(intab, outtab)    -- 字符串中要替代的字符组成的字符串。          outtab -- 相应的映射字符的字符串。    intab = "swhtr"    outtab = "12345"    name = "hjjksknsnjmk" name.maketrans(intab, outtab) 返回结果:{104: 51, 114: 53, 115: 49, 116: 52, 119: 50}

Partition

    功能:根据指定的分隔符将字符串进行分割。        如果字符串包含指定的分隔符,则返回一个3元的元组,第一个为分隔符左边的子串,第二个为分隔符本身,第三个为分隔符右边的子串。    name = ‘swht‘    li = ‘hhsslswhtolljm‘    li.partition(name)    返回结果:(‘hhssl‘, ‘swht‘, ‘olljm‘)

Replace

    功能:把字符串中的 old(旧字符串) 替换成 new(新字符串),如果指定第三个参数max,则替换不超过 max 次。    语法:str.replace(old, new[, max])    参数:old -- 将被替换的子字符串。         new -- 新字符串,用于替换old子字符串。         max -- 可选字符串, 替换不超过 max 次 str = "this is string example....wow!!! this is really string" str.replace("is", "was") 返回结果:‘thwas was string example....wow!!! thwas was really string‘ str.replace("is", "was", 3) 返回结果:‘thwas was string example....wow!!! thwas is really string‘

Split

    功能:字符串分割,默认是空格    name.split()    返回结果:[‘swht‘]    name.split(‘s‘) #以‘s‘字符进行分割 返回结果:[‘‘, ‘wht‘]

**__add__**

    功能:在字符串后面增加指定的字符或字符串    ‘swht‘    name.__add__(‘e‘)    返回结果:‘swhte‘    li = ‘hjh‘    name.__add__(li)    返回结果:‘swhthjh‘

**__contains__**

    功能:判断指定字符串是否包含在字符串中,返回值为True和False    name = ‘swht‘    name.__contains__(‘s‘)    返回结果:True

**__eq__**

    功能:判断字符串是否相等,返回值为True和False    name = ‘swht‘    li = ‘test‘    name.__eq__(li)    返回结果:False

Python action string (2)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.