python 字符串学习

“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()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
学习Python字符串的过程中,我发现了一些有用的方法和技巧。首先,我学会了使用capitalize()方法将字符串的首字母大写,以及使用swapcase()方法将字符串的大小写互换。此外,我还学会了使用endwith()和startwith()方法来检查字符串是否以特定的结尾或开头开始。另外,我还学会了使用find()和rfind()方法来查找并返回字符串中特定字符的索引位置。我还学会了使用isnumeric()方法来检查字符串是否只包含数字字符。此外,我还学会了使用ljust()和rjust()方法来对齐字符串的左侧或右侧。我还学会了使用lstrip()和rstrip()方法来删除字符串左侧或右侧的字符。另外,我还学会了使用partition()方法来找到字符串中的特定子字符串并将字符串分成三个元组。如果字符串中不包含该子字符串,则后两个元组为空。我还学会了使用replace()方法将字符串中的旧字符替换为新字符,并可以指定替换的次数。此外,我还学会了使用strip()和split()方法来切割字符串。最后,我还学会了使用format()方法来格式化字符串。通过这些学习,我对Python字符串的使用有了更深入的理解。\[1\] 此外,我还学到了一些关于字符串的其他知识。例如,我了解到可以使用'''或"""来创建包含换行的字符串。我还了解到在字符串中嵌套引号也会被计算为字符数。另外,我还了解到可以使用str()内置类将其他类型的数据转换为字符串。例如,可以使用int()将字符串转换为整数。\[2\]\[3\] 总的来说,通过学习Python字符串,我对字符串的操作和处理有了更深入的了解,并且掌握了一些实用的方法和技巧。这些知识将对我在编程中处理字符串时非常有帮助。 #### 引用[.reference_title] - *1* [2020-9-20 Python 学习心得(字符串)](https://blog.csdn.net/qq_43478017/article/details/108700839)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [python字符串方法小结](https://blog.csdn.net/JaneFree/article/details/123242522)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值