Python: Methods for Strings

Source: Internet
Author: User

1. Search for Strings (Find;index;rfind;rindex):
S.find (substr, [Start, [end]])
#返回S中出现substr的第一个字母的标号, returns-1 if there is no substr in S. Start and End function is equivalent to searching in s[start:end]
S.index (substr, [Start, [end]])
#与find () is the same, but when there is no substr in S, a run-time error is returned
S.rfind (substr, [Start, [end]])
#返回S中最后出现的substr的第一个字母的标号, if there is no substr in S, then return-1, that is, the first occurrence of substr from the right of the first letter marking
S.rindex (substr, [Start, [end]])

2. Substitution of strings (Replace,strip,rstrip,lstrip)
S.replace (Oldstr, Newstr, [Count])
#把S中的oldstar替换为newstr, Count is the number of replacements. This is a common form of substitution, and there are some functions for replacing special characters
The Strip method is responsible for the kinsoku of the string, and the intermediate content is not. If you want to remove the middle of the content, you can use replace, replace with "'
S.strip ([chars])
#把S中头尾chars中有的字符全部去掉, it can be understood to replace s before and after chars to none.
S.lstrip ([chars])
S.rstrip ([chars])

3. Count of Strings (count):
S.count (substr, [Start, [end]]) #计算substr在S中出现的次数

4. Splitting and Grouping of strings (Split,join):
S.split ([Sep, [Maxsplit]])
#以sep为分隔符, divide s into a list. Maxsplit represents the number of splits. The default delimiter is a blank character
S.rsplit ([Sep, [Maxsplit]])
S.splitlines ([keepends])
#把S按照行分割符分为一个list, Keepends is a bool value that retains the row delimiter after each row is true.
' Connectors '. Join (SEQ) #把seq代表的序列-string sequence, concatenated with a connector

5. String content, Format judgment: (returns TRUE or false)
S.startswith (Prefix[,start[,end]) #是否以prefix开头
S.endswith (Suffix[,start[,end]) #以suffix结尾

S.isalnum () #是否全是字母和数字, and has at least one character
S.isalpha () #是否全是字母, and has at least one character
S.isdigit () #是否全是数字, and has at least one character
S.isspace () #是否全是空白字符, and has at least one character
S.islower () #S中的字母是否全是小写
S.isupper () #S中的字母是否便是大写
S.istitle () #S是否是首字母大写的

6. String-Case conversion (lower,upper,swapcase,capitalize):
S.lower () #小写
S.upper () #大写
S.swapcase () #大小写互换
S.capitalize () #首字母大写

7. Alignment of the string output (Just,center,zfill):
S.ljust (Width,[fillchar])
#输出width个字符, S left-aligned, insufficient parts are filled with Fillchar, the default is a space.
S.rjust (Width,[fillchar]) #右对齐
S.center (width, [Fillchar]) #中间对齐
S.zfill (width) #把S变成width长, and right-aligned, less part with 0 complement

Python: Methods for Strings

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.