python 简单字符串操作

# 切分字符串language = 'Python and java and C++ and Golang and Scala'#split 切割字符串 生成一个列表: 暂时理解为一个容器 有序序列result = language.split("and")
print(result)#2. 链接序列 生产字符串 跟split 是相反的操作
lang = ["English", 'chinese', 'jananese']# 通过 - 连接上面的语言 形成字符串
result2='-'.join(lang)
print(result2, type(result2))#3.删除字符串两边的空格 strip
class_name = ' Big Data '
print(len(class_name))
# 删除两边的空格
class_name_new=class_name.strip()
print(class_name_new, len(class_name_new))# 4.判断一个在字符串是否以指定字符串开始
mystr = 'hello world'
# mystr 以hello开始 则返回Ture
print(mystr.startswith(('hello')))
# 不是以world开始 放回False
print(mystr.startswith('world'))
# 以world结束 返回True
print(mystr.endswith(('world')))
# 判断在指定范围内是否以hello开始
print(mystr.startswith(('hello',3,8)))
print(mystr.startswith('lo' ,3 ,8))
# 列表 [],然后里面可以是任何类型的数据 12 ,23.6 ,'',[]
#               0        1        2       3     4
name_list = ['james', '蔡徐坤', '罗志祥', '格林', 2022]
# len 表示列表长度
print(name_list, type(name_list), len(name_list))
# 1. 列表索引查找
print(name_list[0])
print(name_list[1])
print(name_list[2])
print(name_list[3])
print(name_list[4])# 使用index 查找指定的数据 返回指定数据在列表中的位置
print(name_list.index('格林'))
# 在指定的列表范围内查找格林 没有找到则报错
# print(name_list.index('格林'),0,2)# 2.统计一个元素在列表中的个数 count
name_list2 = ['蒋卢', '吴萍雨', '李龙波', '蒋卢']
result1 = name_list2.count('蒋卢')
result2 = name_list2.count('李龙波')
result3 = name_list2.count('饶鹏鹏')
print(result1, result2, result3)# 3.计算列表长度
print(len(name_list))
print(len(name_list2))# 4.判断指定元素是否存在
name_list3 = ['缪警官', '涛涛', '卢涛', '高宇']
print('涛涛' in name_list3)
print('杨主峰' in name_list3)
print('覃喜文' not in name_list3)
print('卢涛' not in name_list3)# 5.增加一个元素在列表中 加到列表的末尾name_list3.append('杨主峰')
print(name_list3)# 追加一个序列  将一个列表整体加入到列表中
name_list3.append(['孙涛', '张恩'])
print(name_list3)# 追加一个序列 将序列中的值一个一个加入进去
name_list3.extend(['峰峰', '庆庆'])
print(name_list3)# 在指定的位置上插入一个数据
name_list3.insert(1, '良好', )
print(name_list3)


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部