Python里的String,PythonString
分享于 点击 42251 次 点评:285
Python里的String,PythonString
1.字符串对象
s1 = str()
print(s1) # ' ' 即得到一个为空的字符串对象
s1 = str()
print(s1) # 打印为 '' 即得到一个为空的字符串对象
---------------------
# in python `''` or `""` is the same
字符串的 ” “ ' ' ‘’‘ ’‘’‘都是字符串 没有字符这一个概念。都是字符串对象。
比如字符串取一个字符其实还是本质是字符串的概念。没有字符的概念。一个多个都是字符串。
2.字符串求长度
s2 = "cesto" # 'cesto'
s2len = len(s2)
3.切片运算:就是切出来子字符串。无论多少个字符都是字符串
s2[-3:] # "sto"
从倒数第三个开始到结束
s2[
2:5] # "sto" 就是从0开始数 第2至5个。
s3 = s2[:
3] # 'ces'从0开始数 从0到第3-1个。
s3 += 'to' # return 'cesto' 字符串拼接
4.List列表
# list in python is same as ArrayList in java
s2list = list(s3) #把字符串变成list
# string at index 4
s2[4] # 'o' 从0开始数 第4个
# find index 查找下标的办法。
s2.index('w') # return 5, if not found, throw ValueError
s2.find('w') # return 5, if not found, return -1
【总结和拓展】
在Python里面,没有StringBuffer 或者 StringBuilder。
但是在Python
里面处理String本身就比较 cheap。不太消耗资源。
相关文章
- 暂无相关文章
用户点评