>>seasons = ['Spring', 'Summer', 'Fall', 'Winter'] >>> list(enumerate(seasons)) [(0, 'Spring'), (1, 'Summer')," />

飞道的博客

python 字符串学习

442人阅读  评论(0)
“apple”<"banana"
True
按照元素顺序比较

序列转换
tuple()
str()
list()

可迭代对象:元组,列表,没有字符串!

enumerate()

>>>seasons = ['Spring', 'Summer', 'Fall', 'Winter']
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons, start=1))       # 下标从 1 开始
[(1, 'Spring'), (2, 'Summer'), (3, 'Fall'), (4, 'Winter')]
>>>a = [1,2,3]
>>> b = [4,5,6]
>>> c = [4,5,6,7,8]
>>> zipped = zip(a,b)     # 打包为元组的列表
[(1, 4), (2, 5), (3, 6)]
>>> zip(a,c)              # 元素个数与最短的列表一致
[(1, 4), (2, 5), (3, 6)]
>>> zip(*zipped)          # 与 zip 相反,*zipped 可理解为解压,返回二维矩阵式
[(1, 2, 3), (4, 5, 6)]
a_string="hello!i want to know,yes?"
cnt=0
for i in a_string:
  if i in "?!,":
    cnt+=1
print("there are %d punctuations in the sentence"%(cnt))
or print("there are [} punctuations and {} words in the sentence".format(cnt,wrdcnt))

format 格式化字符串

s t r.split(" ")返回按照空格分割的列表
str.replace(“the”,“that”)把str中的the替代成that
" ".join(alist)把alist中的字符串用空格连接起来

st=b'\xe6\x89\x8e\xe5\xbf\x83\xe4\xba\x86\xef\xbc\x8c\xe8\x80\x81\xe9\x93\x81'
st.decode()

转载:https://blog.csdn.net/Viviancoder/article/details/104738166
查看评论
* 以上用户言论只代表其个人观点,不代表本网站的观点或立场