• Python基础三


    1.有变量name = "aleX leNb",完成以下操作

    name = "aleX leNb"
    # 1)移除name变量对应的两边的空格
    s1 = name.strip(' ')
    # 2)移除name变量左边‘al’
    s2 = name[2:]
    s3 = name.lstrip('al')
    # 3)移除name变量右边‘Nb’
    s4 = name[:7]
    s5 = name.rstrip('Nb')
    # 4)移除name变量开头的‘a’和结尾的‘b’
    s6 = name[1:-1]
    # 5)判断变量是否以‘al’开头
    s7 = name.startswith('al')
    # 6)将name变量对应的所有的‘l’转换为‘p’
    s8 = name.replace('l','p')
    # 7)将name变量对应的所有的‘l’分割
    s9 = name.split('l')
    # 8)将name变量值变大写
    s10 = name.upper()
    # 8)将name变量值变小
    s11= name.lower()
    # 9)将name变量首字母大写
    s12 = name.capitalize()
    # 10)判断name变量‘T’出现几次
    s13 = name.find('T')
    # 11)从变量name中找到‘N’所在的索引,不在则报错
    s14 = name.index('N')

    2.使用while和for循环分别打印字符串s="asdfer"

    s = "asdfer"
    i = 0
    while i < len(s):
        print(s[i])
        i = i+1
    for i in s :
        print(i)

    3.实现一个整数加法计算器 如:content=input() 如果用户输入5+9或5+ 9或者 5 + 9然后进行分割运算

    content = input("请输入内容")
    s = content.split('+')
    s1 = s[0]
    s2 = s[1]
    s1 = int(s1.strip(' '))
    s2 = int(s2.strip(' '))
    print(s1+s2)

    4.计算用户输入的内容有几个整数 如:content=input()

    content = input("请输入内容")
    count = 0
    for i in content:
        if i > '0' and i < '9':
            count += 1
    print(count)
  • 相关阅读:
    maven
    sublime text
    ios category
    python
    activiti
    出版社
    gradle
    selenium
    spring
    Ansible状态管理
  • 原文地址:https://www.cnblogs.com/aizh/p/11031937.html
Copyright © 2020-2023  润新知