1. String removal space
# # Strip (self, Chars=none) #去除字符串两端空格 # Lstrip (self, chars=none) #去除字符串左端空格 # Rstrip (self, chars=none) #去除字符串右端空格程序: str1 = "Hello world! "Print Str1.strip () print Str1.lstrip () print str1.rstrip () run result: Hello World!hello world! Hello world!
2. Uppercase and lowercase conversions in strings
# lower (self) #全转换为小写 # upper (self) #全转换为大写 # swapcase (self) #大小写互换 # capitalize (self) #只有字符串首字母大写, remaining lowercase # title (self) # Convert the first letter of the word to a capital program str2 = "Hello world!" Print Str2.lower () print str2.upper () print str2.swapcase () print str2.capitalize () print str2.title () run Result: Hello world! HELLO world! HELLO world! Hello world! Hello world!
3. Find the string position
# # Find (self, sub, start=none, End=none) Find string first position from left, cannot find return-1, find return index position # index (self, sub, Start=none, End=none) Find the string from the left the first position, can not find an error, find the return index position # RFind (self, sub, Start=none, End=none) starting from the right to find the first position of the string, cannot find return 1, find the return index position # Rindex (self, sub , Start=none, End=none) find the first position of the string from the right, can not find an error, find the return index location program: STR3 = "Hello world!" Print Str3.find ("W", 0, 3,) Print Str3.index ("W", 0, 7,) Print Str3.rfind ("L", 0, 7,) Print Str3.rindex ("L", 0, 7,) run Result: 1633
4. String alignment
# # Rjust (self, width, fillchar=none) Take fixed length right alignment, left not enough space to do # ljust (self, width, fillchar=none) Take fixed length left alignment, right not enough space to complement # Center ( Self, width, fillchar=none) Take fixed length middle alignment, left and right not enough space to complement program: STR4 = "Hello world!" Print Str4.rjust ("-") print str4.ljust (+, "+") print Str4.center (20, "~") Run Result:--------Hello World!hello world!+++++ +++~~~~hello world!~~~~
5. BOOL Judgment
# # Isspace (self) Whether the string is a space # isupper (self) string whether the full uppercase # Islower (self) string is full lowercase # isalnum (self) Whether all letters or numbers # Isalpha (self) is full letter # IsDigit whether the full number # isspace (self) is the title # StartsWith (A. Prefix, Start=none, End=none) has a string beginning with # EndsWith suffix, Start=none, end=none) has a string end program: STR5 = "Hello" print str5.isspace () print str5.islower () print Str5.startswith ( "") Print Str5.endswith ("!") Print Str5.isalpha () print str5.isalnum () print str5.isalpha () print str5.isdigit () print str5.istitle () Running Result: Falsetruefalsefalsetruetruetruefalsefalse
6. String segmentation
# # split (self, sep=none, Maxsplit=none) divided by a symbol, the number of times. # Rsplit (self, sep=none, Maxsplit=none) splits from the right by a symbol, the number of times. # partition (self, SEP:STR) string contains the specified delimiter, it returns a tuple of $3, the first is the substring to the left of the delimiter, the second is the delimiter itself, and the third is a substring to the right of the delimiter. # rpartition (self, sep:str) is similar to the partition () function, but looks from the right. Program: STR6 = "www.baidu.com" Print Str6.split (".") Print Str6.split (".", 1) Print Str6.rsplit (".", 1) str = "http://www.baidu.//com" Print str.partition ("//") print Str.rpartition ("///") operation results: [' www ', ' Baidu ', ' com ' [' www ', ' baidu.com '] [' www.baidu ', ' com '] (' http: ', '//', ' www.baidu./ /com ') (' Http://www.baidu. ', '//', ' com ')
7. String connection
# # Join (self, iterable) concatenate (iterated) tuples or elements in a list, with a character program: List1 = ("Hello", "World",) STR7 = "~" Print Str7.join (list1) Run Result: hello~ World
8. String Count
# # COUNT (self, x, start, end,) the number of occurrences of a string, beginning to end position program: Str8 = "Hello world!" Print Str8.count ("L", 0, 6,) Run Result: 2
9, the TAB key fills the space, the tab is denoted by \ t
# # Expandtabs (self, tabsize=none) turns the tab sign (' \ t ') in the string to a space, the tab symbol (' \ t ') The default number of spaces is 8 program: STR9 = "hello\t\tworld!" Print str9.expandtabs () run Result: Hello world!
10. Substitution/Formatting of strings
# # format (self, *args, **kwargs) Replace placeholder {0} {1} ... # replace (self, old, new, Count=none) replaces the specified number of times with old is new, starting from left program: Str10 = "hel Lo World "Print (" Name:{0} age:{1} ". Format (" Hello "," ten ") Print (" Hello:{name},world:{age} ". Format (name=" Yang ", age=" ")) Str11 =" Hello Hello hello! " Print str11.replace ("Hello", "Hello", 2) Run result: Name:hello Age:10hello:yang,world:20hello Hello hello!
11, Zfill ()
The # # Zfill (self, Width:int) method returns a string of the specified length, the original string is right-aligned, and the front padding is 0. Program: Str12 = "Yang" Print str12.zfill (20) Run Result: 0000000000000000yang
# def decode (self, Encoding:unicode = ..., Errors:unicode = ...) Unicode: ... # def encode (self, encoding:unicode = ..., Errors:unicode = ...) -Str: ... # def splitlines (self, keepends:bool = ...)-LIST[STR]: ... # def translate (self, table:optional[anyst R], Deletechars:anystr = ...) --Anystr: ...
This article is from the "Lifelong Learning" blog, please be sure to keep this source http://20120809.blog.51cto.com/10893237/1979110
Python basic data type string 02