Python's string manipulation method

Source: Internet
Author: User

Definition and Characteristics:

Enclosed in quotation marks (single quotes, double quotes, three quotation marks) and cannot be modified

A= ' \ t aBcdE fgfijdlmnopq rSt utwxy z 123 [email protected]# \ t '

One, Judge string, return bool value: false or True

Print (A.isidentifier ())                    #判断是否可以作为标识符/variables using print (A.isdigit ())                         #判断是否全为整数型print (A.isalpha ())                         #判断是否全为拼音print (A.isalnum ())                         #判断是否为普通字符print (A.isdecimal ())                       #判断是否为十进制字符print (A.islower ())                         #判断字母是否为小写print (A.isupper ())                         #判断字母是否为大写print (A.isnumeric ( ))                       #判断是否为数字print (a.isprintable ())                     #判断字符是否都可以打印. such as disk devices, tabs, etc. are not printable print (A.isspace ())                         #判断是否为空格print (A.istitle ())                        # Determine if the title is the first letter of the word print (A.startswith (' a '))                   #判断字符串是否以指定字符开始print (A.endswith (' G '))                     #判断字符串是否以指Fixed character end 

Second, the segmentation string

1. Return to List

Print (A.split (' ab ', 1)) #以指定字符为分隔符从左往右将字符串切割形成列表, can specify the number of cut print (A.rsplit (' a ')) #以指定字符为分隔符从右往左将字符串切割形成列表, the number of cuts can be specified

2. Return to tuple

Print (A.partition (' a ')) #从右往左以第一个指定字符为分隔将字符串分割为三部分形成元组: Character left, character itself, character right print (A.rpartition (' a ')) #从右往左以第一个指定字符为分隔将字符串分割为三部分形成元组: Character left, character itself, character right

3. Return string

Print (A[-1:1:-2])                                   #根据索引切分, [Start Position: End Position: Step] Note: The end position is not included in the Shard print (A.strip ())                                    #删除字符串两侧的空格, tab, enter, can also specify character print (A.rstrip ())                                    #删除字符串右侧的空格, tab, enter, can also specify character print (A.lstrip ())                                    #删除字符串左侧的空格, tab, enter, or specify the character print ("|". Join ([' other\t ',  ' and\t ',  ' or\t ')])                      #使用self分隔指定的字符. For example, use variable A to separate the string ABC.

Three, search in string

Print (A.index ("B"))                                #找到指定字符的第一次出现的索引位, if not, The error print (A.rindex (' a '))                               #查找指定字符最后一次出现的索引位, No error returned print (A.find ("B"))                                # Finds the index bit for the first occurrence of the specified character, or returns -1print (A.rfind (' l ')) if none                               # Finds the index bit of the last occurrence of the specified character and does not return -1print (A.count (' ad '))                                              #统计指定字符出现的次数

Four, replace, and fill

Print (A.center (+, '-'))                            #两侧填充至指定字符print (A.rjust (+, '-'))                              #左侧填充至指定字符print (A.ljust (+, '-'))                              #右侧填充至指定字符print (A.encode (' Utf-8 '))                           #使用指定编码集编码print (A.replace (' A ', ' I ', 2))                        #查找指定字符更新替换, update specified number A is old, I is new. Replace 2 print (A.swapcase ())                                #大小写交换. lowercase becomes uppercase. Uppercase to lowercase print (a.capitalize ())                              #首字符大写print (A.title ())                                    #单词首字母大写print (A.lower ())                                    #字符串中字符全小写print (A.upper ())                                    #字符串中字符全大写print (A.expandtabs (4))                             #指定制表符长度, default is 8 bytes.

V. Formatted OUTPUT

A = ' My Name ' is {name}, the age is {old} ' print (A.format_map ({' name ': ' Feng ', ' old ': one}) #格式化输出, use the dictionary format to assign the print (A.forma                    T (name= ' Feng ', old=11)) #格式化输出, use variable format to assign a = ' My name is {} ', ' {' {} ', ' ' Print ' (A.format (' Feng ', ' 11 ') #格式化输出, use the default index bit to assign a = ' My Name is {1} ', ' {0} ' print (A.format (' Feng ') ') #格式化输出, assign a value using the specified index bit

Vi. advanced usage.

Output = ' abcd ' input = ' 1234 ' make = Str.maketrans (output,input) #将output中的值映射成input中的值test = ' ABCDEFG ' pri NT (Test.translate (make)) #使用映射值替换self中的值

Summarize:

The first summary, there is not exactly the point. Also ask the great God to point out. Thanks here.

This article is from the "hard to go off" blog, please be sure to keep this source http://302876016.blog.51cto.com/12889292/1944365

Python's string manipulation method

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.