zoukankan      html  css  js  c++  java
  • Python字符串练习

    #1字符串:格式化输出、字符串分割、字符串搜索、连接
    from math import pi

    import string
    """
    format_test = "2017 %% %s %s %.3f"
    value = ("hello","world",pi)
    print(format_test % value)

    #简单转换
    str1 = "The egg is $%d %x %f %i %r %.1f %5.3f,%.5s" % (42,15,pi,pi,42,pi,pi,"123456789")
    print(str1)
    #符号对齐0填充
    str2 = "%010.3f" % (pi)
    str3 = "%-10.3f" % (pi)
    print(str2)
    print("左对齐",str3,"右侧空格")
    str4 = "% 5d" % 10 , "%+5d" % -10, "%+5d" % 10
    print(str4)

    width = input("please input your ")
    price_width = 10
    item_width = int(width) - price_width
    header_format = "%-*s%*s"#*作为字段宽度或精度宽度会从元组读取(%-25s左对齐右侧空格补齐 %10s)
    format = "%-*s%*.2f"
    print("="*int(width))
    print(header_format % (item_width,"item",price_width,"price"))
    print("-"*int(width))
    print(format%(item_width,"Apple",price_width,0.4))
    print(format%(item_width,"Apple",price_width,0.4))
    print(format%(item_width,"Apple",price_width,0.4))
    print(format%(item_width,"Apple",price_width,0.4))
    print(format%(item_width,"Apple",price_width,0.4))
    print("-"*int(width))
    print("="*int(width))
    """

    """
    输出结果
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    2017 % hello world 3.142
    The egg is $42 f 3.141593 3 42 3.1 3.142,12345
    000003.142
    左对齐 3.142 右侧空格
    (' 10', ' -10', ' +10')
    please input your 100
    ====================================================================================================
    item price
    ----------------------------------------------------------------------------------------------------
    Apple 0.40
    Apple 0.40
    Apple 0.40
    Apple 0.40
    Apple 0.40
    ----------------------------------------------------------------------------------------------------
    ====================================================================================================
    """



    """
    #2find方法:查找子串返回子串所在位置最左侧索引,没找到返回-1
    str5 = "hello world welcome you are good hello"
    print(str5.find("you"))
    print(str5.find("your"))
    print(str5.find("he"))
    print(str5.find("hello"))
    print(str5.find("we"))
    print(str5.find("llo",3))#3为从起始点3开始查找
    print(str5.find("llo",3,30))#3,30为从起始点3到结束点30之间查找
    """

    """
    输出结果
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    20
    -1
    0
    0
    12
    35
    -1
    """

    """
    #3 join方法:在队列中添加元素
    str6 = ['1','2','3','4']#队列元素必须是字符串,不能是数字
    str7 = "+"
    print("连接字符串",str7.join(str6))
    """
    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    连接字符串 1+2+3+4
    """

    """
    #4 lower:返回字符串的小写版
    str7 = "Hello World Welcome You Are Good"
    print("返回小写字母:",str7.lower())
    #查找names
    name = "Hello"
    names = ["hello","Hi","world"]
    if name.lower() in names :
    print("Find it")
    #title:所有单词首字母大写
    print("welcome to china that's very good".title())
    print(string.capwords("welcome to china that's very good"))
    """

    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    返回小写字母: hello world welcome you are good
    Find it
    Welcome To China That'S Very Good
    Welcome To China That's Very Good
    """

    """
    #5 replace 返回匹配替换后的字符串
    str7 = "Hello World HeHe"
    print("字符串替换后",str7.replace("HeHe","!"))
    """

    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    字符串替换后 Hello World !
    """

    """
    #6 split:将字符串分割成序列
    print("1+2+3+4+5+6+7".split("+"))
    print("D:嵌入式-书籍中转站编程书籍".split('\'))
    print("以空格或者制表符分割:","12 34 56 78 0".split())
    """

    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    ['1', '2', '3', '4', '5', '6', '7']
    ['D:', '嵌入式-书籍中转站', '编程书籍']
    以空格或者制表符分割: ['12', '34', '56', '78', '0']
    """

    """
    #7 strip:除去两侧的空格(不包含内部)
    print(" Hello World ".strip())
    print("除去两侧指定的字符:","###**** Hello *** world ***###!".strip("*!#"))
    """

    """
    C:python3.7python.exe D:/Python-Test/qiubai/qiubai/Test2.py
    Hello World
    除去两侧指定的字符: Hello *** world
    """

    """
    #8 translate:替换字符串中的某些部分,只处理单个字符,可以同时替换多个
    from string import maketrans # 引用 maketrans 函数。

    intab = "aeiou"
    outtab = "12345"
    trantab = maketrans(intab, outtab)

    str = "this is string example....wow!!!";
    print str.translate(trantab);
    """

    """
    th3s 3s str3ng 2x1mpl2....w4w!!!
    """
  • 相关阅读:
    用bat更改hosts文件批处理
    docker 清理容器的一些命令,彻底或选择清理
    Hbase+Phoenix环境部署和使用
    使用内置tomcat的pom配置
    git修改已提交记录的用户信息
    Linux清空正在运行的log日志文件内容
    Maven打包: 包含依赖jar和指定Main class
    docker-compose 搭建 kafka
    Java URLEncoder 兼容 js encodeURIComponent
    linux查找删除大文件
  • 原文地址:https://www.cnblogs.com/acer-haitao/p/7262126.html
Copyright © 2011-2022 走看看