字串操作
1.字串擷取
s = 「hello」
print(s[0:3])
(1)擷取全部字元
s = 「hello」
print(s[:])
(2)倒序擷取字元
s = 「hello」
print(s[::-1])
(3)中有乙個冒號時,冒號左右數字代表擷取的起點和終點,步長預設為1;若冒號兩邊沒有數字,則擷取字串的全部內容。
(4)中有兩個冒號時,三個引數分別代表起點、終點和步長。步長為負值時從右向左擷取。
2.去空格
s = " hello "
print(s.strip())(去左右空格)
print(s.lstrip())(去左空格)
print(s.rstrip())(去右空格)
注:字串中間的空格不能刪除。
3.字串複製
s = 「hello」
s_copy = s
print(id(s))
print(id(s_copy))
注:複製前後位址相同,型別相同,操作方法相同
4.字串連線
(1)s = 「hello」
s1 = 「python」
s2 = s + s1
print(s2)
(2)import operator
s1 = operator.concat(s,s1)
print(s1)
5.比較(比較首字母的順序)
s = 「hello」
s1 = 「python」
import operator
b = operator.lt(s,s1)
lt(a,b):小於
le(a, b) ———— 小於等於
eq(a, b) ———— 等於
ne(a, b) ———— 不等於
ge(a, b) ———— 大於等於
gt(a, b) ———— 大於
6.字串長度
print(len(s),len(s1))
字串最大、最小字元
s = 「hello」
s1 = 「python」
print(max(s))
print(min(s1))
7.字串大小寫轉換
s = 「hello python i love you」
print(s.upper()) 轉大寫
print(s.lower())轉小寫
print(s.title())每個單詞首字母大寫
print(s.capitalize())整個字串首字母大寫
print(s.swapcase())大寫轉小寫,小寫轉大寫
8.字串分割
s = 「hello python how are you」
ss = s.split() 按空格拆分成列表
ss = s.split(「o」) 按()中的字母拆分成列表
9.字串序列連線
s = 「hello python how are you」
s1 = (「ghjkl」)
s2 = s.join(s1)
print(s2)
10.列表轉字串
s = [「hello」,「word」]
str = 「-」
print(str.join(s))
11.字串內查詢
(1)查某個單詞首次出現的首字母的下標
s1 = 「today is a fine day」
index = s1.find(「is」)
print(index)
在某個範圍內查詢某個單詞首次出現的首字母的下標,如果沒有則輸出-1
s1 = 「today is a fine day」
index = s1.find(「is」,6,8)
print(index)
12.字串內替換
s1 = 「today is a fine day is is is」
s = s1.replace(「is」,「are」,2) ()內引數為:舊的(需要被替換的)引數 新的引數 替換數量
print(s)
創造新的儲存空間進行修改
13.字串判斷
s = " d "
print(s.isspace())
14.可以用單引號或雙引號表示字串。單個單引號和雙引號只能表示單行,三單引號或三雙引號可以表示多行資料。
四種字串型別
s = 『asdf』
s1 = 「asdf」
s2 = 「」" 「」"
s3 = 『』』 『』』
python學習日誌
1 python中range xrange 和np.arange 區別 range 多用於迴圈,返回乙個range物件,若想要返回乙個list則前面加上list轉換 arange 是numpy中的函式,np.range 返回乙個array型別的物件,可以使用小數步長 xrange 返回xrange ...
Python學習日誌
元組不可修改,但是可以給儲存元組的變數賦值。現在每學乙個東西就會莫名的拿去和之前學過的東西做一比較,python是真的太舒服了。當然寫for or if 都是要記得後面的 而且也可以進行級聯,只不過是if,elfe,else。還可以用in來判斷某個元素在沒有某個列表裡面。字典是一系列鍵值對,新增起來...
python 學習日誌
1 pip is already installed if you re using python 2 2.7.9 or python 3 3.4 binaries downloaded from python.org,but you ll need to upgrade pip 2 upgradi...