zoukankan      html  css  js  c++  java
  • 字符串使用

    # 字符串的使用
    # 字符串里的元素:单个字母、字符串取值数字、汉字、单个符号都是一个元素
    # len(数据)统计数据长度print(len(s))
    # 字符串取值:字符串名[索引值]
    # 索引从左至右是0、1、2、3....,从右往左是-1、-2、-3....
    # s = "hello!"
    # print(s[5])
    # print(s[-1])
    
    # 字符串取多个值:切片 字符串名[索引头:索引尾:步长] 索引头默认0,步长默认1
    # s[1:5:1]取头不取尾,取值是ello
    # print(s[1:5:2])  # 1, 3,5,但是不取尾
    # print(s[:])  # 取全部
    # print(s[:4])
    # print(s[3:])
    
    # 利用切片倒序输出s
    # print(s[-1:-7:-1])
    # print(s[::-1])
    
    
    # 字符串的分割 .split(可以指定分割符) 转换成了列表,只能字符串使用
    # 指定的切割符,被切走了
    # print(s.split())
    # s1 = " hello"
    # print(s1.split("e"))
    # print(s1.split("l", 1))  # 切走遇到的第一个l,切割一次
    # # 字符串的替换 .replace(指定替换值,新值,替换次数)
    # new = s.replace("l", "@")
    # print(new)
    
    # 字符串去除指定字符 .strip(字符串)
    # 默认去掉空格
    # 只能去掉头和尾的指定字符
    # s = " hello!"
    # print(len(s))
    # print(s)
    # new = s.strip()
    # print(new)
    #
    # new1 = s.strip("!")
    # print(new1)
    
    # 字符串的拼接 + 只能将字符串和字符串拼在一起
    # s_1 = "python11"
    # s_2 = "节日快乐"
    # print(s_1 + s_2)
    # ,表示轮流输出两个变量
    # print(s_1, s_2)
    # s_3 = 6
    # print(s_1, s_2, s_3)
    # print(s_1 + s_2 + str(s_3))
    
    
    # 字符串格式化输出 % format
    # age = 18
    # name = "星"
    # score = 99.99
    # print("python11的" + name + "今年" + str(age) + "岁!")
    
    # 格式化输出1:format 特点{} 用{}占位
    # print("python11期的{},今年{}岁!".format(name, age))
    # print("python11期的{1},今年{0}岁!".format(name, age))
    
    # 格式化输出2:% %s字符串 %d数字 %f浮点数
    # %s 可以填任何数据
    # %d 只能填数字:整型、浮点数,输出整型数据,会去掉小数点后的
    # %f 可以填数字,%.1f可以指定小数位数
    # print("python11期的%s,今年%d岁!" % (name, age))
    # print("python11期的%s,今年%s岁!" % (name, age))
    # print("python11期的%s,今年%s岁!考试考了%s" % (name, age, score))
    # print("python11期的%s,今年%s岁!考试考了%d" % (name, age, score))
    # print("python11期的%s,今年%s岁!考试考了%f" % (name, age, score))
    # print("python11期的%s,今年%s岁!考试考了%.2f" % (name, age, score))
    # print("python11期的%s,今年%s岁!考试考了%.1f" % (name, age, score))
    
    
    # find 查找字符串中的字符、子字符串,返回字符的索引
    # s = "hello"
    # print(s.find("l"))  # 返回第一个l的索引
    # print(s.find("lo"))  # 返回lo字符串中l的索引
    # print(s.find("qw"))  # 如果字符串不存在,则返回-1
    
    # replace(old, new) 替换字符串
    # 注意必须是字符串替换字符串,不能是数字去替换字符串
    s = "hello"
    print(s.replace("lo", "ha"))  # 用ha替换lo
  • 相关阅读:
    数据库连接池Druid使用总结
    Mysql中查看每个IP的连接数
    解Bug之路-Druid的Bug
    python 安装python-memcached and pylibmc两个模块
    memcache
    python 交互式执行SQL
    tomcat内存泄漏存入dump文件
    MySQL SQL优化之覆盖索引
    【 Tomcat 】tomcat8.0 基本参数调优配置
    配置路由器/交换机的Telnet登录
  • 原文地址:https://www.cnblogs.com/come202011/p/12232290.html
Copyright © 2011-2022 走看看