怎么自己创立网站,竞价推广教程,网站开发 百度网盘,wordpress比特币平台字符串特点
1. 只可以存储字符串 2. 长度任意 (取决于内存大小) 3. 支持下标索引 4. 允许重复字符串存在 5. 不可以修改 (增加或删除元素等) 6. 支持for和while循环
字符串的下标索引
# 字符串的下标索引
从前向后#xff0c;下标从0开始
从后向前#xff0c;下标从-1开始…字符串特点
1. 只可以存储字符串 2. 长度任意 (取决于内存大小) 3. 支持下标索引 4. 允许重复字符串存在 5. 不可以修改 (增加或删除元素等) 6. 支持for和while循环
字符串的下标索引
# 字符串的下标索引
从前向后下标从0开始
从后向前下标从-1开始
语法字符串[下标]
例子
my_str hello world# 取第一个
value1 my_str[0]
print(value1)# 取最后一个
value2 my_str[-1]
print(value2)my_str[2] 2 # 不能修改
查找特定字符串的下表索引值---index
# 查找特定字符串的下表索引值---index()
语法: 字符串.index(字符串)print(my_str.index(h))
字符串替换
# 字符串的替换
语法 字符串.replace(字符串1, 字符串2) ----------- 字符串1替换为字符串2
注意不是修改字符串本身而是得到一个新的字符串
例子
my_str hello world
new_str my_str.replace(hello,hello1)
print(my_str) # hello world
print(new_str) # hello1 world
字符串的分割
# 字符串的分割
语法: 字符串.split(分隔符字符串)
功能: 按照指定的分隔符字符串将字符串划分为多个字符串并存入列表对象中
注意:字符串本身不变而是得到了一个列表对象
例子
my_str1 hello python hello world
new_str1 my_str1.split( )
print(new_str1) # [hello, python, hello, world]
print(my_str1) # hello python hello world
字符串的规整操作
# 字符串的规整操作去前后空格
语法字符串.strip()# 字符串的规整操作去前后指定字符串
语法字符串.strip(字符串)
例子
my_str2 hello python hello world
print(my_str2)
print(my_str2.strip())my_str3 12hello python hello world21
print(my_str3.strip(12))
统计某字符串的出现次数 --- count
my_str4 121232564862
count my_str4.count(2)
print(count) # 4
统计字符串长度 --- len
my_str5 121232564862
num len(my_str5)
print(num) # 12