python string method more complete version 200 310

String Manipulation

Because too many methods, only part of the presentation

1) determining the type - 9

method Explanation
string.isspace() If the string contains only blanks, True is returned
string.isalnum() If there is at least one character string and all the characters are letters or numbers True Returns
string.isalpha() If there is at least one character string and all the characters are letters True Returns
string.isdecimal() If the string contains only digit returns True,全角数字
string.isdigit() If the string contains only digit returns True, 全角数字, ,\u00b2
string.isnumeric() If the string contains only digit returns True, 全角数字,汉字数字
string.istitle() If the string is the title of (each word is capitalized) True Returns
string.islower() All of these (case sensitive) if the character string contains at least one alphanumeric characters, and all lowercase, True is returned
string.isupper() If the string contains at least one of alphanumeric characters, and all of these (case-sensitive) characters are uppercase, True is returned

2) Find and Replace --7

method Explanation
string.startswith(str) Check if the string begins with str, that returns True
string.endswith(str) Check if the string str is ending, is it returns True
string.find(str, start=0, end=len(string)) Str string is included in the detection, if the start and end the specified range, it is checked whether contained within the specified range, if the index value of the return, otherwise -1
string.rfind(str, start=0, end=len(string)) Similar to the find (), but from the beginning to find the right
string.index(str, start=0, end=len(string)) Similarly with the find () method, but not in the string will get an error if str
string.rindex(str, start=0, end=len(string)) Similar to the index (), but starting from the right
string.replace(old_str, new_str, num=string.count(old)) Replace the string into new_str old_str, if num is specified, the replacement does not exceed num times

3) Case conversion --5

method Explanation
string.capitalize() The first character uppercase
string.title() Each word capitalized string
string.lower() Conversion string in all uppercase characters to lowercase
string.upper() Conversion string of lowercase letters to uppercase
string.swapcase() Flip-sensitive string of

4) text alignment --3

method Explanation
string.ljust (width) Returns a string of the original left-aligned and padded with spaces to the width of the new string length
string.rjust(width) Returns a string of the original right-aligned and padded with spaces to the width of the new string length
string.center(width) Returns a string of the original center, and padded with spaces to the width of the new string length

5) removing whitespace characters --3

method Explanation
string.lstrip() Truncated string left (start) whitespace
string.rstrip() Cut off the right side of string (end) whitespace
string.strip() Truncated string left and right sides of the whitespace characters

6) Split and connected --5

method Explanation
string.partition(str) String into a string element 3-tuple (str front, str, behind str)
string.rpartition(str) 类似于 partition() 方法,不过是从右边开始查找
string.split(str="", num) 以 str 为分隔符拆分 string,如果 num 有指定值,则仅分隔 num + 1 个子字符串,str 默认包含 ‘\r’, ‘\t’, ‘\n’ 和空格
string.splitlines() 按照行(’\r’, ‘\n’, ‘\r\n’)分隔,返回一个包含各行作为元素的列表
string.join(seq) 以 string 作为分隔符,将 seq 中所有的元素(的字符串表示)合并为一个新的字符串

7) 编码与解码 - 5

方法 说明
string.encode() 把字符串 string 分成一个 3 元素的元组 (str前面, str, str后面)
bytes.decode() bytes数据类型转变为string数据类型

8)格式化字符串的输出

方法 说明
string.format() 把数据填入到字符串的槽中。
string1 + string2 字符串1 + 字符串2
发布了63 篇原创文章 · 获赞 1 · 访问量 784

Guess you like

Origin blog.csdn.net/whalecode/article/details/104768534
Recommended