字串翻轉
【方法1】
【方法2】str
="123"
(str
)str=''
.join(
reversed
(str))
(str
)
字串去空格/特殊字元str
="123"
(str
)str
=str[:
:-1]
(str
)
字串拼接s=
' hello,world hh '
('*'
,s,'*'
('*'
,s.strip(),
'*')
('*'
,s.lstrip(),
'*')
('*'
,s.rstrip(),
'*')
('*'
,s.replace(
' ','')
,'*'
('*',""
.join(s.split(
' ')),
'*')
輸出* hello,world hh *
* hello,world hh *
* hello,world hh *
* hello,world hh *
* hello,worldhh *
* hello,worldhh *
字串比較s=
' hello,world hh '
s1='jadl'
(s)print
(s1)
s+=s1
(s)輸出
hello,world hh
jadl
hello,world hh jadl
字串內容判斷s1=
"helloworld"
s2="helloworle"
s3='hello'
s4='hello'
(s1==s2)
(s1>s2)
(s1print
(s3==s4)
print(id
(s3)
==id
(s4))輸出
false
false
true
true
true
#特別說明:cmp()在py3中已經被移除了
字串內容查詢s1=
"123"
s2="hello"
s3=s1+s2
s4="hello world"
s5="hello world"
s6="hello world"
s7=" "
#isalnum()判斷所有的字元都是數字或字母
(s1.isalnum())
(s2.isalnum())
(s3.isalnum())
("*************************=="
)#isalpha()判斷所有字元都是字母
(s1.isalpha())
(s2.isalpha())
(s3.isalpha())
("*************************=="
)#isdigit() # 判斷所有字元都是數字
(s1.isdigit())
(s2.isdigit())
(s3.isdigit())
("*************************=="
)# islower() # 判斷所有字元都是小寫
("小寫?"
,s2,s2.islower())
(s4,s4.islower())
(s5,s5.islower())
(s6,s6.islower())
("********************************"
)# isupper() # 判斷所有字元都是大寫
("大寫?"
,s2,s2.isupper())
(s4,s4.isupper())
(s5,s5.isupper())
(s6,s6.isupper())
#istitle() # 判斷所有單詞都是首字母大寫,像標題
(s2,s2.istitle())
(s4,s4.istitle())
(s5,s5.istitle())
(s6,s6.istitle())
("-------------------"
)# isspace()) # 判斷所有字元都是空白字元、\t、\n、\r
(s6,s6.isspace())
(s7,s7.isspace(
))
字串大小寫轉換s=
"hello world"
(s.index(
'l')
(s.index(
' ')
(s.index(
'll'))
輸出:2
52
查詢字串s=
"hello world"
(s.upper())
(s.lower())
輸出:hello world
hello world
字串分割s=
"hello world"
(s.find(
"llo"))
(s.find(
"x")
)輸出:2-
1
s=
"1,2,3,4,5,6,7,8"
rs=s.split(
',')
(rs)
輸出:[
'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8']
python學習筆記(一) 字串
字串是python中最常用的資料型別。我們可以使用引號 或 來建立字串。建立字串很簡單,只要為變數分配乙個值即可。例如 var1 hello var2 world python不支援單字元型別,單字元在python中也作為乙個字串使用。python訪問子字串,可以使用方括號來擷取字串,例如 通過索引...
字串(一) 字串Hash
今天開一手最不 tao 擅 yan 長的字串演算法 字串hash演算法。似乎提到字串的話,kmp應該是更為常見的一種,但是hash有它的優點,被犇們稱為 優雅的暴力 何謂hash?hash的中文稱為雜湊,這當然是音譯,直譯過來就是雜湊,或者也有叫預對映的。雜湊的作用就是通過某個特殊函式的對映,將任意...
Python 一 字串常用操作
python的字串不可改變 可以像對陣列操作一樣訪問字串的部分內容 s 123 獲取全部內容s 123 獲取從某一下標到結尾的全部內容s 1 23 獲取從開頭到某一下標之前的全部內容s 2 12 獲取乙個左閉右開區間的內容s 1 2 2 再來乙個冒號表示步長s 0 3 2 13 負數代表倒數第n個元...