程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

STR module of Python notes

編輯:Python
01 Converts the specified object to a string
str(object=’’) -> str
st01 = 1000
st = str(st01)
02 Create string
str(bytes_or_buffer[, encoding[, errors]]) -> str
st02 = 'chengdu'
st = str(b'abc',encoding='utf-8')
03 The first character of a string is capitalized
capitalize(self, /)
st03 = 'henan is very good'
st = st03.capitalize()
04 Convert a string to a format that is easy to compare
casefold(self, /)
st04 = 'ThewaetherIS Very HOt'
st = st04.casefold()
05 String centered and filled
center(self, width, fillchar=’ ', /)
st05 = 'Welcome'
st = st05.center(20,'-')
06 Query the number of times the substring appears in the string , Start and end positions can be specified
S.count(sub[, start[, end]]) -> int
st06 = 'abcdefghijkabcdefghijk'
st = st06.count('ka',1,-1)
07 Converts a string to in the specified encoding format bytes type
encode(self, /, encoding=‘utf-8’, errors=‘strict’)
st07 = ' Zhengzhou, Henan '
st = st07.encode('utf-8')
08 Judge whether the specified position of the string is a substring
S.endswith(suffix[, start[, end]]) -> bool
st08 = 'Welcome'
st = st08.endswith('come',3)
09 All the \t Extend to the specified bit length
expandtabs(self, /, tabsize=8)
st09 = 'Welcome\tto\tBeiJing\t'
st = st09.expandtabs(4)
10 Search the substring with unknown start and end specified in the string, and return the position index of the first letter of the substring
S.find(sub[, start[, end]]) -> int
st10 = 'abcdefghijkabcdefghijk'
st = st10.find('abc',10,-1)
11 String formatting
S.format(*args, **kwargs) -> str
st11= '{}, Hello , Welcome to {}.'
st = st11.format(' Han Meimei ',' Beijing ')
st11 = '{1}, Hello , Welcome to {0}.'
st = st11.format(' Beijing ',' Han Meimei ')
12 Format string by keyword
S.format_map(mapping) -> str
st11 = '{name}, Hello , Welcome to {city}.'
st = st11.format(city=' Beijing ',name=' Han Meimei ')
13 Gets the first occurrence of a substring at the index of the first letter with an unknown start and end
S.index(sub[, start[, end]]) -> int
st13 = ' Han Meimei , Hello , Welcome to Beijing .'
st = st13.index(' welcome ',1,-1)
14 Determine whether the string consists entirely of letters ( Protect Chinese characters ) And numbers
isalnum(self, /)
st14 = '12345a and '
st = st14.isalnum()
15 Determine whether the string consists entirely of letters ( Protect Chinese characters )
isalpha(self, /)
st15 = 'aaaa and '
st = st15.isalpha()
16 Determine whether the string consists entirely of ascii Code composition
isascii(self, /)
st16 = ' Beijing '
st = st16.isascii()
17 Determine whether the string is all composed of numbers
isdecimal(self, /)
st17 = '1234.1'
st = st17.isdecimal()
18 Determine whether the string is a numeric string , Decimal point... Is not supported
isdigit(self, /)
st18 = '1235'
st = st18.isdigit()
19 Determine if the string is python Reserved words
isidentifier(self, /)
st19 = 'def'
st = st19.isidentifier()
20 Determine whether the string is all lowercase
islower(self, /)
st20 = 'beijing'
st = st20.islower()
21 Determine whether the string is a numeric string , Decimal point... Is not supported
isnumeric(self, /)
st21 = '1212'
st = st21.isnumeric()
22 Determine whether the string is a printable type
isprintable(self, /)
st22 = str(set((1,2,3)))
st = st22.isprintable()
23 Determine whether the string is an empty string
isspace(self, /)
st23 = ' '
st = st23.isspace()
24 Determine whether the string is a title type
istitle(self, /)
st24 = 'The Title Is Henan'
st = st24.istitle()
25 Determine whether the string is capitalized
isupper(self, /)
st25 = 'HELLOq'
st = st25.isupper()
26 Connect an iteratable object with a specified string
st26 = ('www','baidu','com')
st = '.'.join(st26)
27 Specify the left alignment of the string and display the specified number of bits. Fill in the insufficient number of bits with the specified string
ljust(self, width, fillchar=’ ', /)
st27 = 'Hello'
st = st27.ljust(20,'-')
28 Lowercase the specified string
lower(self, /)
st28 = "Hello Han Meimei ,Welcome"
st = st28.lower()
29 Remove the custom string to the left of the string
lstrip(self, chars=None, /)
st29 = ' hello welcome -'
st = st29.lstrip('-')
30 Find the specified separator from the left side of the string , Before the return delimiter 、 Separator 、 The ancestor of the last three parts of the separator
partition(self, sep, /)
st30 = 'hello, Han Meimei welcome'
st = st30.partition(',')
31 If the string starts with the specified substring, the substring removed part is returned , Otherwise return the original string
removeprefix(self, prefix, /)
st31 = 'hello, Han Meimei welcome'
st = st31.removeprefix('hello')
32 If the string ends with a specified substring, the substring removed part is returned , Otherwise return the original string
removesuffix(self, suffix, /)
st32 = 'hello, Han Meimei welcome'
st = st32.removesuffix('welcome')
33 Use new Substring replaces... In the string old Substring
replace(self, old, new, count=-1, /)
st33 = 'hello, Han Meimei welcome'
st = st33.replace(' Meimei ',' Snow snow ')
34 Returns the maximum index of the substring in the string
S.rfind(sub[, start[, end]]) -> int
st34 = 'abcdefghijkabcdefghijk'
st = st34.rfind('d',1,-1)
35 Returns the maximum index of the substring in the string
S.rindex(sub[, start[, end]]) -> int
st35 = 'abcdefghijkabcdefghijk'
st = st35.rindex('d',1,-1)
36 Aligns the string to the right and displays the specified length , If the length is not enough, use the specified string to fill
rjust(self, width, fillchar=’ ', /)
st36 = 'rjust with tab'
st = st36.rjust(30,'-')
37 Find the separator from the right side of the string , Use the delimiter to split the string into pre split 、 Separator 、 After the division of the three parts of the Yuanzu
rpartition(self, sep, /)
st37 = ' Xinyang, Henan Province '
st = st37.rpartition(' province ')
38 Splits the string from the right with the specified string , Returns a list of split strings , The division length can be specified
rsplit(self, /, sep=None, maxsplit=-1)
st38 = 'hello,Lee,welcome,to,china'
st = st38.rsplit(',',2)
39 Remove the specified substring on the right side of the string
rstrip(self, chars=None, /)
st39 = 'welcome --'
st = st39.rstrip('-')
40 Splits the string from the left with the specified string , Returns a list of split strings , The division length can be specified
split(self, /, sep=None, maxsplit=-1)
st40 = 'hello,Lee,welcome,to,china'
st = st40.split(',',2)
41 Break a string with a newline character , You can specify to display line breaks
splitlines(self, /, keepends=False)
st41 = 'Hello Lee \n Welcom to china'
st = st41.splitlines()
42 Determine whether the string starts with a substring
S.startswith(prefix[, start[, end]]) -> bool
st42 = 'helloleewelcometochina'
st = st42.startswith('hello')
43 Returns the string after removing the specified substring around the string
strip(self, chars=None, /)
st43 = ' helloleewelcometochina '
st = st43.strip(' ')
44 Is the exchange of upper and lower case characters in a string
swapcase(self, /)
st44 = 'Hello Lee,welcome to china'
st = st44.swapcase()
45 Convert the string to title Type string
title(self, /)
st45 = 'the title is test'
st = st45.title()
46 One to one conversion of the characters in the string through the decoding table
translate(self, table, /)
st46 = 'ETC fast E Quick pass '
ss = str.maketrans('ETC',' Electronic records ')
st = st46.translate(ss)
47 Capitalize all characters of the string
upper(self, /)
st47 = 'the title is test'
st = st47.upper()
48 use 0 The coordinates of the filled numeric string form the specified length
zfill(self, width, /)
st48 = '121.88'
st = st48.zfill(12)
49 Return to a forever translate Decoding table of
maketrans(…)
st49 = str.maketrans('abc','ABC')
st = st49
50 String inversion
st50 = 'chengdu'
st = st50[::-1]

print(st)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved