zoukankan      html  css  js  c++  java
  • 字符串练习题(1)

    #判断一句话中,没有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'

     
  • 相关阅读:
    npm改为淘宝镜像
    html中table中td内容换行
    git 切换文件夹路径
    git经常使用的命令
    day16
    day15
    day13
    day14
    day12
    day11
  • 原文地址:https://www.cnblogs.com/wenm1128/p/11619744.html
Copyright © 2011-2022 走看看