New Pythoner即學即會的常見函式集結

2021-10-09 12:33:27 字數 4087 閱讀 3911

1 . abs( ):求絕對值

print

(abs(-

54))# 54

2 . round(m,n):四捨五入 m為需要四捨五入的數,n為保留小數

print

(round

(2.45676583,3

))# 2.457

3 . max( ):求最大值

print

(max(23

,43,75

,16,89

,3,2

,88))

# 89

4 . min( ):求最小值

print

(min(23

,43,75

,16,89

,3,2

,88))

# 2

5 . pow(m,n):求次方 m為底數,n為指數

print

(pow(3

,3))

# 9

6 . lower( ):將字元轉換為小寫

舉例:str=「how are you」

print

(str

.lower())

# how are you

7 . upper( ):將字元轉換為大寫

print

(str

.upper())

# how are you

8 . title( ):將每個單詞第乙個字元轉換為大寫

print

(str

.title())

# how are you

9 . capitalize( ): 首先字母大寫,其餘小寫

print

(str

.capitalize())

# how are u

10 . swapcase( ):大小寫字母互換

print

(str

.swapcase())

# how are u

11 . eval(str)函式:將字串轉換為有效的表示式來求值,如轉換為list列表,dict字典。

print

(eval

("[1,2,3,4,6]"))

# [1, 2, 3, 4, 6]

print

(eval(''

))# print

(eval

("12-3"))

# 9

12 . 填充函式

舉例:str = 「you are so great」

12-1 . str.center(n, " * "):n為加上填充符後的字串長度(包括空格),「 」裡是想要填充的字元,center函式時左右各填充(n-原字串長度)/2個符號

print

(str

.center(26,

"*")

)# *****you are so great*****

12-2 . str.ljust(n," * " ): 在右邊填充 ,n為加上填充符後的字串長度(包括空格),「 」裡是想要填充的字元,ljust函式時右邊各填充(n-原字串長度)個符號

print

(str

.ljust(26,

"*")

)# you are so great**********

12-3 . str.rjust(n," * " ): 在左邊填充 ,n為加上填充符後的字串長度(包括空格),「 」裡是想要填充的字元,ljust函式時左邊各填充(n-原字串長度)個符號

print

(str

.rjust(26,

"*")

)# **********you are so great

12-4 . str.zfill(n):原字元右對齊,前面預設補0,n為補齊後字串長度(包括空格)

print

(str2.zfill(26)

)# 0000000000you are so great

13 . str.count(str2, start, end)函式:求str2在str1**現的次數,可以指定下標範圍

str1 =

'good'

str2 =

'good good good study'

print

(str2.count(str1,0,

20))# 3

14 . str2.find(str1,start.end)函式:從左往右檢測str1在str2第一次出現的下標位置

str1 =

'nice'

str2 =

'nice nice very nice to meet you'

print

(str2.find(str1,0,

20))# 0

15 . str2.rfind(str1,start,end)函式:從右往左檢測str1在str2第一次出現的下標位置

str1 =

'nice'

str2 =

'nice nice very nice to meet you'

print

(str2.rfind(str1,0,

20))# 15

16 . str.join():以指定字串作為分隔符,將所有元素合併為乙個新的字串

list1 =

["hello"

,"nihao"

]print(""

'++'

.join(list1)

)# hello++nihao

17 . 擷取和分離字串

17-1 . str.lstrip(char)函式:擷取左邊指定字元

str

="*******how are you*****"

print

(str

.lstrip(

'*')

)# how are you*****

17-2. str.rstrip(char)函式:擷取右邊指定字元

str

="*******how are you*****"

print

(str

.rstrip(

'*')

)# *******how are you

17-3 . str.split(str1,num):分離字串

str

="i want to see you"

print

(str

.split(

'to'))

# ['i want ', ' see you']

18 . replace( ):字串的替換

str

="how are you"

print

(str

.replace(

'are'

,'do'))

# how do you

19 . str.startswith/endswith( ):判斷字串的開頭和結尾

str1 =

'hello python'

print

(str1.startswith(

'h')

)# true

print

(str1.endswith(

"h")

)# false

天學網 生活即教育,教育即生活

當代中國教育,核心素養是學生的必備素養,讓學生在提高自身能力的基礎上,勇敢地gjllzwxq面對社會,學會如何在社會上立足。自教育改革以來,就把培養學生的興趣和各方面的綜合素質放到了重要位置。英語作為學生語言表達和交流的工具是這門學科核心素養的關鍵,要盡可能地讓學生用所學到的英語做事情,從而培養他們...

PHP 即點即改

show.php 頁面 鏈結資料庫 link mysqli connect 127.0.0.1 root root mfour 設定字符集 mysqli query link,set names utf8 拼接sql sql select from user 執行 res mysqli query ...

無框架即點即改

在這其中簡單做了下分頁 header content type text html charset utf 8 page get page dsn mysql host 127.0.0.1 dbname php8 ecshop db new pdo dsn,root root array pdo m...