zoukankan      html  css  js  c++  java
  • python学习-string


    # 字符串
    str_my = "hello,python!我来了!"

    # 读取,通过索引来读取。从0开始。
    # 取某一个具体的值
    # print(str_my[0])
    # # 取区间值 字符串变量名[起始索引:结束索引]。包含起始,但不包含结束。
    # #前5个羊肉串
    # print(str_my[0:4]) # 0,1,2,3
    # print(str_my[0:5]) # 0,1,2,3,4
    # # 从第6个开始,一直取到最后。
    # print(str_my[5:])
    # # 从头开始,取到索引下标为7
    # print(str_my[:8])
    # print(str_my[0:8])
    #
    # # 字符串有多长。
    # print(len(str_my))

    # 1、查找子符串。字符串变量名.find(子字符串)
    # 如果找到了,返回的是,起始索引。如果没有找到了,返回的是,-1.
    # python
    print(str_my.find("python")) #6
    # ph
    print(str_my.find("ph")) #-1
    #!
    print(str_my.find("!")) #12


    #2、替换操作。 字符串变量.replace(old,new)
    # 用$来替换!
    new_str = str_my.replace("!","$",1)
    print(str_my)
    print(new_str)

    #3、大小写转换
    nn_str = new_str.upper()
    print(new_str)
    print(nn_str)

    #4、删除字符串 左右(头尾) 两边的空格,或者指定的字符串。
    # 字符串变量名.strip([指定的字符串])
    str_a = " 11python17,class3 "
    #删除头尾的空格
    new_str = str_a.strip()
    print(str_a)
    print(new_str)
    # 删除头尾指定的11
    new_2 = new_str.strip(" 1")
    print(new_2)
    # 扩展:lstrip,rstrip

    # 5、符串截段,按照指定的分隔符。例如,作文当中的排比名的分隔符;
    # 字符串变量名.split(分隔符)
    str_b = "大家好,我是小简老师。今天的天气真好,上课很开心。一会儿就中场休息。"
    result = str_b.split("。")
    print(str_b)
    print(result)
    # 指定分隔的次数
    res_2 = str_b.split("。",1)
    print(res_2)

    # 6、拼成字符串。要求:列表当中每一个值都要是字符串。
    # 用连接符,将列表当中字符串拼成一个字符串。
    # 连接符.join(列表)
    list_a = ['大家好,我是小简老师', '今天的天气真好,上课很开心', '一会儿就中场休息', '']
    cc = " &&& !!".join(list_a)
    print(cc)

    # 7、格式化字符串
    # % %s 字符串 %d 数字 %f 浮点数
    str_cc = "我今年的目标是:薪资上涨 %d ,达到月薪 %d " % (5000,15000)
    print(str_cc)

    # format 占位符{}
    str_dd = "我今年的目标是:薪资上涨 {} ,达到月薪 {} ".format(5000,15000)
    print(str_dd)

    # 占位符{0}{1}
    str_ff = "我今年的目标是:薪资上涨 {1} ,达到月薪 {0} ".format(15000,5000)
    print(str_ff)
    str_66 = "我今年 {0} 岁,我希望我每年都是 {0} 岁。".format(18)
    print(str_66)

  • 相关阅读:
    经历:如何设置jquery easyui中下拉框不可编辑
    经历:easyui的layout自适应高度布局
    JavaScript高级程序设计(九):基本概念----函数
    JavaScript高级程序设计(九):基本概念----语句的特殊点
    JavaScript高级程序设计(八):基本概念--操作符
    JavaScript高级程序设计(三):基本概念:数据类型
    JavaScript高级程序设计(七):JavaScript中的in关键字
    JavaScript高级程序设计(六):关键字 void 和 delete 使用
    JavaScript高级程序设计(五): js的关键字instanceof和typeof使用
    SQL Server 存储过程
  • 原文地址:https://www.cnblogs.com/qsmyjz/p/11261168.html
Copyright © 2011-2022 走看看