python基礎語法總結(三)-- 數與字串
阿新 • • 發佈:2018-12-22
python基礎語法總結(一)-- python型別轉換函式+檔案讀寫
目錄
一. 數
1. 基本操作
'''數型''' # 八進位制表示 a = 0o123 # python八進位制用字首 0o表示,與C、java的字首0稍有不同 print(a) # 十進位制83 # 複數 complex m = 9 + 3j #複數定義 # 整數相除的精度問題 print(3/2) #小數1.5 (python3) print(3//2) #整數1 (python3) # n次方 print(3**2) # 求平方 得到9 #浮點數精度損失 print(2.3 - 1.3) # float計算時,有可能會出現浮點數精度損失的問題 # python對大整數的支援 a = 99**99 # a特別大,已經超過了int和long的儲存範圍,但python儲存和處理都無壓力
2. 數的處理:math模組
'''math模組''' from math import * x = 0.5 ; y=0.5 sin(x) # 即math.sin(),求正弦 cos(x) # 餘弦 asin(x) # 反正弦 acos(x) # 反餘弦 tan(x) # 正切 atan(x) # 餘切 hypot(x,y) # 直角三角形的斜邊長度 fmod(x,y) # 求x/y的餘數 ceil(x) # 取不小於x的最小整數 floor(x) # 取不大於x的最大整數 fabs(x) # 求絕對值 exp(x) # 求e的x次冪 pow(x,y) # 求x的y次冪 log10(x) # 求以10位底的x的對數 sqrt(x) # 求x的平方根 pi # π 3.1415926...
3. 隨機數 random
### 隨機數
import random
print(random.random()) # 產生 0 到 1 之間的隨機浮點數
print(random.randint(1, 100)) # 產生 1 到 100 的一個整數型隨機數
print(random.uniform(0.1, 7.9)) # 產生 0.1 到 7.9 之間的隨機浮點數,區間可以不是整數
print(random.choice('hello world')) # 從序列中隨機選取一個元素
print(random.randrange(1,100,5) ) # 生成從1到100的間隔為5的隨機整數
二. 字串
1. 定義
'''
字串
'''
message = "hello world~" #變數賦值語句,不需要var、public、int等關鍵字
a1, a2, a3 = '1', '2', '3' # 多變數賦值
print(message) #列印語句,本例中,控制檯輸出:hello world~
2. 字串屬性
### 字串屬性
type(message) #得到變數的實際資料型別,本例:str
len(message) #返回字串長度,本例:12
message.count('o') #某個子字串在其中出現的次數,未出現為0,本例:2
message.find('w') #某個子字串在其中第一次出現的位置,不存在為-1,本例:6
message.join("123") #連線字串,將原字串插入引數字串中每兩個字元之間 本例: '1hello world~2hello world~3'
str.join(message,'123') # 連線字串,本例: '1hello world~2hello world~3'
message.split() # 分割字串,預設用空格分割,本例:['hello', 'world~']
message.split('o') # 分割字串, 本例:['hell', ' w', 'rld~']
message * 3 # 複製字串,再拼接。 本例:'hello world~hello world~hello world~'
3. 字串分片
# 字串分片(擷取)
message[1:3] # 擷取第二、三個字元的新字串
message[3:-2] # 擷取第四到倒數第三個字串。本例:'lo worl'
4. 字串判斷
### 判斷
'86GEsdEW'.isalnum() # 檢測字串是否只包含 0-9A-Za-z 本例:True
'86GEsd&EW'.isalnum() # 因有'&' 本例:False
'B23dds'.isalpha() # 檢測字串是否僅包含 A-Za-z(純字母),本例:False
"Hello".isalpha() # 本例True
'B23dds'.isdigit() # 檢測字串是否僅包含數字
'123'.isdigit() # 本例False
message.isspace() # 檢測所有字元是否均為空白,本例:False
' '.isspace() # 本例True
message.istitle() # 檢測字串中單詞是否為首字母大寫
message.isupper() # 檢測字串是否都為大寫
'or' in message # 判斷message中是否存在:'or'子字串,本例:True
5. 大小寫轉換
### 大小寫轉換,原字串不會變,只是返回新值
message.upper() #字串中字母全部轉為大寫返回,本例:HELLO WORLD~
message.lower() #字串中字母全部轉為小寫返回,本例:hello world~
message.title() #字串每個單詞首字母大寫,本例:Hello World~
message.capitalize() #句子中首個單詞大寫,本例:Hello world~
message.center(30) #將字串居中,並使用空格填充至長度 width 的新字串,本例:' hello world~ '
message.swapcase() #字串大小寫全部翻轉,本例:hELLO wORLD~
6. 去除空格
### 去除空格,字串本身沒變
str_ = ' example word '
str_.strip() #兩端去空格,本例:'example word'
str_.rstrip() #右側(right)去空格,本例:' example word'
str_.lstrip() #左側(left)去空格,本例:'example word '
7. 格式化字串
### 格式化字串
# 在python中,可以在字串中使用以 % 開頭的字元,使得指令碼中改變字串中的內容 #
"""
%c : 單個字元
%d : 十進位制數字
%o : 八進位制數字
%s : 字串
%x : 十六進位制數字,其中字母小寫
%X : 十六進位制數字,其中字母大寫
"""
template = "Your name is %s"
print(template % '小明') # 列印:Your name is 小明,
'%s is %d years old' % ('小紅', 20) # 多個引數替換 結果:'小紅 is 20 years old'
# 原始字串,不再轉義
s = r"c:\user\123.txt" #字串前加r,則字串中轉義不再生效