對字串的每個元素進行編號:下標從0開始,每個下標都是整數
s = 「hello」通過下標來取出字串中的元素#下標範圍: 0-4
#字串名字[下標]下標為負,從右邊第乙個:-1到左邊第乙個-ns = 「world」
print(s[2]) # r
s = 「hello」替換replaces[-1] # o
s[-5] # h
s = 「hello python,hello world」分割split:標誌存在,按標誌切分,標誌不存在,整個字串不分割s.replace(",", " 「) # 將,替換為空格,此時s本身沒有改變
s = s.replace(」,", " ") # 通過重新賦值,s本身改變
spplit()中不寫任何標誌,預設按空格分割,且會去除空字串
拼接 join:將每個單詞按照標誌連線在一起
分割字串中所有單詞
s = 「hello python, hello world, hello china」#1、標誌不統一,替換統一
s = s.replace(",", " 「)
#2、 分割字串
l = s.split()
l: [『hello』, 『python』, 『hello』, 『world』, 『hello』, 『china』]
#3. 按 「-」 拼接l
#-」.join(l)
#"+".join(l)
Python資料型別 字串型別
變數名 str 變數值 msg hello world print msg 0 print msg 1 msg hello n print len msg msg hello world print ello in msg print lo w not in msg res print hello ...
Python資料型別 字串
字串 1 python 預設的檔案編碼都是ascii,所以要在編碼的時候加上coding utf 8,中文才不會亂碼。len 函式 是計算字串的長度。正確編碼的長度。b 中文 len b 長度是4 a 中文 decode gbk 或utf 8 print len a 長度是2 2 字串前加r 是不轉...
python資料型別(字串)
計算機人們日常事務的輔助工具,在程式設計中也映 現實世界的分類,因此計算機中也引入類別以便進行抽象分析 數字 字串 元組 列表 字典 int 表示的範圍 2,147,483,648 到 2,147,483,647 例如 0,100,100 num 2147483647 type num 一旦超出,則...