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

Python data type -- string

編輯:Python

formatting method

a.capitalize() title case
a.casefold()   All lowercase
a.lower() All lowercase
a.upper() All variable capitals
a.swapcase() Size swap
a.title() The first word of each word is capitalized
a.center(50,'-') Fill left and right
a='jJak\ti' a.expandtabs(20) contain \t String specifying how many spaces
a.ljust(20,'*') Add on the right 20 individual * Number
a.rjust(20,'*') Left supplement 20 individual * Number
a.zfill(20) Insufficient string 20 Bit added on the left 0, Added to 20 position
a.strip() Remove the space on the left and right ,\n \t
a.ltrip() Remove the space on the left side ,\n \t
a.rtrip() Remove the space on the right side ,\n \t
 

Judgment method

a='abcd

a.startswith('A') With A start , Case sensitive

a.endswith('d')

a.isdigit() Integers , return true

a.isalpha() Letter ( Including Chinese ), return true

a.isalnum() Letter ( Including Chinese ) Or digital , return true

a.islower() Judge whether all are lowercase

a.isupper() Determine whether all are capitalized

a.istitle() Determine whether the first word of each word is capitalized

Count 、 lookup 、 Replace 、 modify

a='ABCDEFG'

a.find('G') Left to right , find G, Return subscript value , No return found -1

a.find('G',4,20) From 4 Digits to 20 digit , find G, Return subscript value , No return found -1

a.rfind('G') From right to left , find G, Return subscript value , No return found -1

a.index similar find, But if you can't find it, you will report an error

a.count Usage is similar. , Count the number of strings that appear

a.split('D',1) , The default is to cut with spaces , Returns a list of , The number indicates the number of cuts

a.rsplit Cut from the left

a.removeprefix('A') Remove the prefix

a.removesuffix('A') Remove the suffix

a.replace('D','F',1) D Switch to F, Just change it once , If it is not filled in, the default is to replace all

a=['A','b','c']

'-'.join(a) use “-” String concatenation , return :A-b-c, Variable a, Can be a string , Tuples , list

reversed reverse

aa=' One, two, three '
print(list(reversed(aa)))
print(aa[::-1],type(aa[::-1]))
aa=(' first place ',' Second ',' Third ')
print(tuple(reversed(aa)))
aa=[' first place ',' Second ',' Third ']
print(list(reversed(aa)))
Be careful :reversed() Function returns an iterator , Support string , Tuples , list ,
Return an object ,, So we need to list/tuple Function to convert the corresponding data type
Return results :
[' 3、 ... and ', ' Two ', ' One ']
Three, two, one <class 'str'>
(' Third ', ' Second ', ' first place ')
[' Third ', ' Second ', ' first place ']


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