1.字符串遍历与定义
# 1.字符串遍历与定义
strl = "hello,python!"
strl1 = '我的外号是"大西瓜"'
print(strl1)
print(strl[6])
for char in strl1:
print(char)
"""作者qq:2246656202
csdn:https://blog.csdn.net/qq_39460362
个人网站:www.yzsrw.com"""
首先定义两个字符串,然后第一个可以用print进行弹出,也可以弹出指定索引的值,类似于列表,元祖,字典
然后简单的用了个for循环遍历,然后循环内容。
代码运行效果如图。
2.字符串的统计操作
# 2.字符串的统计操作
hello_str = "hello,world!!!"
# 统计字符串长度
print(len(hello_str))
# 统计小(子)字符串出现的次数
print(hello_str.count("l"))
print(hello_str.count("o"))
# 某一个字符串出现的位置
print(hello_str.index("llo"))
# 如果使用index的方法传递的子字符串不存在 程序会报错的
print(hello_str.index("abc"))
print(hello_str)
"""作者qq:2246656202
csdn:https://blog.csdn.net/qq_39460362
个人网站:www.yzsrw.com"""
首先定义个一个字符串,然后内容我这里是“hello,world!!!”,然后统计长度和列表,元祖,字典都是一样的len。然后print进行弹出。然后统计小的字符串也是同上和列表,元祖,字典一样,只是在count里面加上统计的字符。
查询某个字符串的位置,可以像列表,元祖,字典直接用index输入数值,然后进行弹出索引,找到位置。但是如果输入的索引没有的话,程序会报错的哦!
3.字符串的判断方法
# 3.字符串的判断方法
""" 1判断字符
# 判断空白字符 1
space_str = " " # 如果里面是空格的话 则返回True
print(space_str