python學習筆記05(字串與list)

2021-08-15 14:08:10 字數 591 閱讀 8329

字串與list一樣可遍歷、索引、切片、連線

但不能通過索引更改字元

# -*- coding: utf-8 -*-

"""created on sun feb 11 13:33:59 2018

@author: myself

"""word = "helloworld"

for i in (word): #字串的遍歷

print(i)

print(word[1]) #字串的索引訪問

print(word[-1])

print(word[0:2]) #字串的切片

newword = ",".join(word) #連線字元

print(newword)

#錯誤:word[0] = "i"

#字串不能通過索引更改字元

runfile('f:/python/list30.py', wdir='f:/python')he

llow

orld

edheh,e,l,l,o,w,o,r,l,d

PHP學習筆記(05) 字串

1.字串的定義方式 2.單雙引號的對比,轉義,解析,速度 3.常用函式 header content type text html charset utf 8 heredoc nowdoc定義文字 str3 str3,str4 intro 遠看山無色,近聽水無聲 intro echo str4 no...

python學習筆記3 字串

1.python當中的字串是乙個序列,可以用str i 返回字串中的各個字元。i為0或正數時,是從前向後的第i 1個字元 i為負數時,是倒數第 i個字元。想遍歷整個字串,無需先計算字串的長度再迴圈,可以很方便的使用for語句 for char in string print char 2.strin...

Python學習筆記(3) 字串

字串本身含有但雙引號 python中對於字串只需要將其用 或者 括起來即可,但是如果字串本身包含 或者 時,應該怎麼解決呢?下面用乙個例子來說明解決方法 例如 mary said i m fine 這樣表示 mary said i m fine 簡要的說就是在每乙個但雙引號的前面加上乙個 即可。字串...