#判断一句话中,没有a的单词的有几个!
>>> s="I am a boy!" >>> s.split() ['I', 'am', 'a', 'boy!'] >>> word_list = s.split() >>> result = 0 >>> for i in word_list: ... if "a" not in i: ... result +=1 ... >>> result 2
#题目:把句子中偶数位置的字母后面加“*”输出
>>> s="I am a boy!" >>> result ="" >>> for i in range(len(s)): ... print(i) ... if i%2==0: ... if (s[i]>="a" and s[i]<="z") or (s[i]>="A" and s[i]<="Z"): ... result =result+s[i]+"*" ... 0 1 2 3 4 5 6 7 8 9 10 >>> print(result) I*a*o* >>>
#题目:第一个字母和最后一个字母的拼接结果,用切片完成
>>> s="abcdefg" >>> s[0]+s[-1] 'ag'
>>> s[:1]+s[-1]
'ag'
#前两个字符和后两个字符拼成一个字符串
>>> s[:2]+s[:-3:-1] 'abgf'
>>> s[:2]+s[-2:]
'abfg'