Python string manipulation

Source: Internet
Author: User

First, intercept the string

Str[0:3] #截取第一位到第三位的字符

str[:] #截取字符串的全部字符

Str[6:] #截取第七个字符到结尾

Str[:-3] #截取从头开始到倒数第三个字符之前

STR[2] #截取第三个字符

STR[-1] #截取倒数第一个字符

STR[::-1] #创造一个与原字符串顺序相反的字符串

STR[-3:-1] #截取倒数第三位与倒数第一位之前的字符

Str[-3:] #截取倒数第三位到结尾

Str[:-5:-3] #逆序截取

Second, delete: Show only letters and numbers

def onlycharnum (s,oth= "):

S2 = S.lower ();

Fomart = ' abcdefghijklmnopqrstuvwxyz0123456789 '

For C in S2:

If not C in Fomart:

s = S.replace (c, ");

return s;

Print (Onlystr ("a000 aa-b"))

Third, scan string

S1 = ' Cekjgdklab '

S2 = ' Gka '

NPos =-1

For C in S1:

If C in S2:

NPos = S1.index (c)

Break

Print (NPos)

Four, go to space and special symbol: S.strip (). Lstrip (). Rstrip (', ')

Five

mystr = "Hello World Sky 123"

S1 = ' Sky '

Len (MyStr and S1)

MyStr = Mystr[::-1]

mystr = mystr[::-1]                                    #翻转字符串
Mystr.find (S1)                                            #
Mystr.index (S1)                                         #

MyStr + = S1 #连接字符串, connect S1 to mystr; connect string of specified length: MyStr + = S1[0:n]

Mystr.find (S1) #find从左向右查字符串, returns a subscript when found, returns 1 when not found, does not affect the execution of the program
Mystr.index (S1) #index从左向右查字符串, when found to return subscript, can not find the times wrong, affecting the execution of the program
Mystr.rfind (S1) #rfind从右向左查字符串, returns a subscript when found, returns 1 when not found, does not affect the execution of the program
Mystr.rindex (S1) #rindex从右向左查字符串, when found to return subscript, can not find the times wrong, affecting the execution of the program
Mystr.count (S1) #计算mystr字符串中s1字符串的个数, returns 0 when not present
Mystr.replace ("World", "World", 2) #替换字符串, because the string is non-modifiable, the result of the output is new, the original mystr has not been modified. The string has more than one default all modified, and if the third parameter has a number, it is replaced a few times.
Mystr.split ("") #分号中是从字符串中选取的用作切割的东西, but this thing is lost after the separation.
Mystr.capitalize () #让字符串开头第一个字符大写.
Mystr.title () #让字符串中的所有单词第一个字母都大写.
Mystr.startswith (S1) #判断是否以s1开头, returns True and False
Mystr.endswith (S1) #判断是否以s1结尾, returns True and False
Mystr.upper () #将所有字母都变成大写.
Mystr.lower () #将所有字母都变成小写.
Mystr.center (#总共50字符位置) to center the MYSTR string
Mystr.ljust (#左对齐)
Mystr.rjust (#右对齐)
Mystr.lstrip () #去掉左边的空格
Mystr.rstrip () #去掉右边的空格
Mystr.strip () #去掉两边的空格
Mystr.partition ("and") #以and为中心, is divided into three parts, and itself is the middle part.
Mystr.rpartition ("Itcast") #若是有多个字符, with the right character as the benchmark
Mystr.splitlines () #若是字符串中有换行符 (such as a poem), a newline character will be used as a split, output way dictionary.
Mystr.isalpha () #判断是不是纯字母
Mystr.isdigit () #判断是否为纯数字
Mystr.isalnum () #判断是否只包含字母和数字
Mystr.isspace () #判断是否为纯空格
A = "_"

b = ["AA", "BB", "CC"]
A.join (b) #将b中的元素以a中的内容连接起来

Content = "LSA Fdsfds ds SDFs \t\t\t J ds d FSD fds FDS"
A = Content.split ()
Print (a) #如果括号里什么都没有, by default, splits strings with spaces and tabs.

Python string manipulation

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.