字符串知识 ---len ---count --- startswith --- endswith ---split ---strip ---replace ---isalnum ---isalpha ---isdecimal ---find ---index ---captalize,swapcase,title ---conter ---eval ---整数加法计算器

# len   长度 迭代
s1 = "nihaoma"
print(len(s1)) #   list s1  的长度
#  len 的运用
i = 0
while i < len(s1):
    print(s1[i])
    i+=1

    #s1.count          总  数    查看出现的次数
    #s1.startswith    判断是否以...开头
    #s1.endswith      判断是否以...结尾
s1 = "nihaonihaonihao"
re = s1.startswith("n",1,6)
print(re)
# False
re = s1.count("n",1,6)
print(re)
# 1
re = s1.count("n",1,5)
print(re)
# 0
re = s1.count("ni")
print(re)
# 3

    # split      返还的列表  分割
s1 = "nihaonihaonihao"
re = s1.split("a")
print(re)
# ['nih', 'onih', 'onih', 'o']

    # strip   传出去掉空格
s1 = " nihao "
re = s1.strip()
print(s1)
print(re)
#  nihao
# nihao

     # replace   替换
s1 = " *nihao* "
re = s1.replace('*','#')
print(re)
# nihao#

   # isalnum()) #字符串由字母或数字组成
   # isalpha()) #字符串只由字母组成
   # isdecimal()) #字符串只由十进制组成
   # 判断是否有数字组成
s1 = "nihao359k"
re = s1.isalnum()   # True
print(re)

#    # find 查找       返回的找到的元素的索引位置,如果找不到返回-1
s1 = "nihao359k"
re = s1.find("a")  #在第三位 返 3
re = s1.find("l")  #  不存在 返回 -1
print(re)

#    # index   返回的找到的元素的索引位置,找不到报错
s1 = "nihao359k"
re = s1.index('3')    #找到在第 5 位   5
# re = s1.index('8')    #找不到 报错
re = s1.index('3',2,55) # no ou of range  超出范围不会出错
re = s1.index('3')
print(re)


     #captalize,swapcase,title
s2 = "nihao359k"
print(s2.capitalize()) #首字母大写   ------   Nihao359k
print(s2.swapcase()) #大小写翻转    -------  NIHAO359K
s3 = "hi,i,m,a  rapo"
print(s3.title())#每个单词的首字母大写   -----  Hi,I,M,A  Rapo


#     # conter内同居中,总长度,空白处填充
s2 = "nihao359k"
re =s2.center(20,"3") #   s2.center(20,"3")  " "  只能填一个字符
print(re)#    33333nihao359k333333
print(len(re)) # 验证长度  20

     #Eval(str):官方解释为:将字符串str当成标准的表达式求值并返回计算结果
print(eval('5+9'))#  14

#      # 整数加法计算器
content = input('>>>>').strip()
i = content.split('+')
k = 0
for l in i :
    k+=int(l)
print(k)


# 使用while 循环和for循环分别打印字符串s ='miaoge'中每个元素
s ='miaoge'

for i in s:
     print(i)

i = 0
while i<=5:
    print(s[i])
    i+=1

 

posted @ 2020-07-10 21:01  XuanchenLi  阅读(351)  评论(0编辑  收藏  举报