學了這么久了,想總結一下python基礎性知識,寫的不好或者不對的地方,大家也可以留言讓其補充、修改:
1. 字符串的定義:(ctrl+/)注釋/取消注釋
單引號、雙引號、三引號(其中三引號也可以用來做注釋)
str = "aad222"
str = 'ss22'
str = ''' ss222'''
2. 字符串拼接(+號)
print('hello'+'world') ---- helloworld
print('hello'+'3') ---- hello3
print('hello'*3) ----- hellohellohello
print('hello '*3) ---- hello hello hello
print(3,'hello') --- 3 hello
幾種報錯的拼接字符串:
print('hello'+3) --- 報錯。類型不一致。和前面的保持一致(typeerror:must be str,not int)
print(3+'hello') --- 報錯,類型不一致。和前面保持一致(typeerror:unsupported operand type(s) for +:'int' and 'str')
3.序列操作
序列定義:一個序列,若干個元素組成;一般下標從0開始
常用操作:
a.獲取元素
str[10]/str[len(str)-1]/str[-1]
b.求元素個數
len(str)
c. 獲取某元素的下標
str.index('b')
d.切片操作
string[start:end]:從start開始,到end結束,但不包含end
string[start:]:從start開始,到字符串結束
string[:end]:從第一個字符開始,到end位置結束,但不包括end
string[start:end:步長]:步長默認是1
字符串倒序:string[::-1]
正下表:從左向右:0,1,2,3,4
負下標:從右向左:-1,-2,-3,-4
in : 成員運算符 - 如果字符串中包含給定的字符返回 True
可以使用find方法,不等於-1則表示找到
例如: str = 'hello'
'h’ in str ----- True
not in : 成員運算符 - 如果字符串中不包含給定的字符返回 True
例如: str = 'hello'
'a' not in str -- True
r/R : 原始字符串 - 原始字符串:所有的字符串都是直接按照字面的意思來使用,沒有轉義特殊或不能打印的字符。 原始字符串除在字符串的第一個引號前 加上字母"r"(可以大小寫)以外,與普通字符串有着幾乎完全相同的語法。
例如: open(r"d:\project\aa.txt")
% : 格式化字符串
4.Python轉義字符
\(在行尾時) | 續行符 |
\\ | 反斜杠符號 |
\' | 單引號 |
\" | 雙引號 |
\a | 響鈴 |
\b | 退格(Backspace) |
\e | 轉義 |
\000 | 空 |
\n | 換行 |
\v | 縱向制表符 |
\t | 橫向制表符 |
\r | 回車 |
\f | 換頁 |
\oyy | 八進制數,yy代表的字符,例如:\o12代表換行 |
\xyy | 十六進制數,yy代表的字符,例如:\x0a代表換行 |
\other | 其它的字符以普通格式輸出 |
5.字符串常用的方法
1> str.count():計算字符串中包含多少個指定的子字符串
info = "abcdefa"
print (info.count('a')) ---- 2
2> str.startswith():檢查字符串是否以指定的字符串開頭
info = "this is tom"
print (info.startswith("this")) --- True
3> str.endswith() :檢查字符串是否以指定的字符串結尾
info = "this is tom"
print (info.endswith("tom")) -- True
4> str.find() :返回指定的子字符串在字符串中出現的位置
"123456789".find("456") ---- 3
"ok,good,name".find(',') ---- 2 (有多個則返回第一個)
“ok,good,name”.find('o',3) -- 4(指定位置開始找,正序找)
“ok,good,name”.find('o',-1) -- 0(指定位置開始查找:倒序只找最后一個元素)
5> str.isalpha() :檢查字符串中是否都是字母
"abc1".isaplha() -- False
6> str.isdigit() :檢查字符串中是否都是數字
"123123",isdigit() --- True
7> str.join():將sequence序列類型的參數的元素字符串合並(連接到一個字符串)
print("##".join(['i','like','play','football!'])) -- i##like##play##football
print("##".join(('i','like','play','football!'))) -- i##like##play##football
8> str.split():將字符串分割為幾個子字符串,參數為分隔符,返回list列表
被分割的切點會被拿掉
info = 'name is tom '
print(info.split(' ')) ---- ['name','is','tom','']
9> str.lower() :將字符串里面如果有大寫字母的全部轉為小寫字母
10> str.opper():將字符串里面如果有小寫字母的全部轉為大寫字母
11> str.replace():替換字符串里面指定的子字符串
info = "name is tom"
print(info.replace('tom','jack')) --- name is jack
12> str.strip() :將字符串前置空格和后置空格刪除
" good ".strip() --- 'good'
13> str.lstrip():去除前置空格
" good ".strip() --- 'good '
14> str.rstrip():去除后置空格
" good ".strip() --- ' good'
6.字符串的格式化
1> %s,%d,%f,%x
a > %s:字符串
name ='tom'
age = 18
info = "名字是:%s,年齡是:%d"%(name,age)
print (info)
結果:名字是:Tom,年齡是:18
b> %d:十進制
‘%d’%56 --輸出的是字符串‘56’
‘%10d’56 -- 最小寬度,不足空格補齊 ----- 結果:‘ 56’
‘%010d’%56 -- 補零 ------ 結果:'0000000056'
c> %f:浮點數
默認小數點后面6位
“%f”%3.1415926 -- 3.141593
"%.3f"%3.141592 -- 3.142
指定小數點后面位數
"%9.2f"%1234.567890 -- ' 1234.57'
"%09.2f"%1234.567890 -- '001234.57'
d> %x:無符號的十六進制,(x/X代表了轉換后的十六進制字符的大小寫)
"%x"%108 -- '6c'
"%X"%108 --'6C'
"%#X"%108 -- '0X6C'
"%#x"%108 -- '0x6c'
2> .format()
格式: format % values
format:是等待格式化的字符串,由包含%號的占位符的字符串組成的
values:可以是普通的數值對象、字符串對象
tuple,表示多個對象填充format里面的占位符%
a> 順序填充:.format(name,age)值可以多但是不能少,輸出格式左、右、中間對齊::< ,:>,^
b> 下標填值
c> 變量填值
d> 特殊用法,python3.6以后才可以使用,前面加f
下一節為列表,字典常用方法。。。。