b="""nihao
hahah
xixi
"""
输出:
'nihao\nhahah\nxixi\n'
“原字符串”
big=r"this\
hhaha"
big
输出:
'this\\\nhhaha'
还原为unicode字符串
hello =u'hello\u0020world'
hello
输出:
'hello world'
字符串是不可以改变的
字符串可以通过for循环遍历
可以通过索引或者切片访问字符串:一部分,倒序,跳跃
字符串可以转化为另一种序列,比如列表
分隔为多行:splitlines()
搜索字符串出现的次数:count("ttt")
join将序列中的元素以指定的字符连接生成一个新的字符串
如何每次处理一个字符?
str="hello world"
#list函数
list_str = list(str)
list_str
#1.for循环遍历
for c in str:
print(c)
#2.列表推导
result = [c for c in str]
result
#3.map函数
def fun(c):
return c
results=map(fun,str)
result2=list(results)
result2
输出:
h e l l o w o r l d
Out[21]:
['h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'l', 'd']
字符串处理方法及字符处理探讨
博客围绕字符串处理展开,提到字符串不可改变,可通过for循环遍历,能利用索引或切片访问,还能转化为其他序列。介绍了分隔多行、搜索出现次数、连接元素等方法,最后探讨了每次处理一个字符的问题。

被折叠的 条评论
为什么被折叠?



