• 字符串常用方法


    • upper lower :替换大小写
    data = "LiuGuiXiang"
    print(data.upper())		#字符串全部大写,针对英文字母
    print(data.lower())		#字符串全部小写,针对英文字母
    
    #应用
    code = "Adoc"
    usename = input("输入code:")
    if usename.lower() == code.lower():
        print("输入正确")
    else:
        print("输入错误")
    
    • startswith endswith 判断首字母或结尾字母
    data = "LiuGuiXiang"
    
    print(data.startswith("r"))			#判断字符串是否以r开头,返回布尔值
    print(data.startswith("L"))			
    print(data.endswith("g"))			#判断字符串是否以g结尾,返回布尔值
    print(data.endswith("S"))
    print(data.startswith("G",3,6))		#判断字符串索引值3到6的是否以G开头,endswith相同
    
    • replace 替换
    msg = "you never know your luck"
    msg1 = msg.replace("you","we")
    msg2 = msg.replace("you","we",1)
    print(msg1)
    print(msg2)
    
    #msg.replace(old,new,count),替换字符串中的部分字符,可以指定替换次数
    
    • strip 去除空白,空格, ,
    d1 = " 
    123	"
    print(d1)
    print(d1.strip())
    
    #可以去除指定字符
    data = "ads123gbh"
    print(data.strip("gabhds"))
    
    • split 指定分割符,分割成列表
    data = "天气 hello 打"
    data_list = data.split()		#默认分割符为空白
    print(data_list)
    
    #指定分割符
    data = "天气:hello:打"
    data_list = data.split(":")
    print(data_list)
    
    #指定次数
    data = ":天气:hello:打"
    data_list = data.split(":",2)
    print(data_list)
    #第一个:之前分割之后在列表里是空;添加数字指定分隔次数
    
    • join 联合
    data = ["一","二","三"]
    d1 = ":".join(data)
    print(d1)
    
    运行结果:
    一:二:三 
    #前提是列表里都是字符串才可以
    
    • count 计算某个字符出现的次数
    msg = "asdfgaherfvsdfafsafsdgs"
    print(msg.count("a"))
    
    运行结果:
    4
    
    • format 格式化输出
    #第一种用法
    msg = "今天周{},天气{},出门多穿{}".format("六","大雪","衣服")
    print(msg)
    
    #运行结果
    今天周六,天气大雪,出门多穿衣服
    
    
    #第二种用法
    msg = "今天周{0},天气{1},出门多穿{2}".format("六","大雪","衣服")
    print(msg)
    
    #运行结果
    今天周六,天气大雪,出门多穿衣服
    
    
    #第三种用法
    msg = "今天周{day},天气{weather},出门多穿{clothes}".format(day="六",weather="大雪",clothes="衣服")
    print(msg)
    
    #运行结果
    今天周六,天气大雪,出门多穿衣服
    
    • is 判断字符串
    msg = "1234566"
    msg1 = "sadfas"
    msg2 = "123asd"
    print(msg2.isalnum())		#判断字符串由字母和数字组成
    print(msg1.isalpha())		#判断字符串由字母组成
    print(msg.isdecimal())		#判断字符串由十进制组成
    
    #运行结果
    True
    True
    True
    
  • 相关阅读:
    14.3.2.1 Transaction Isolation Levels 事务隔离级别
    ReentrantLock可重入锁
    Lock与synchronized 的区别
    synchronized 与 Lock
    This usually indicates a missing no-arg constructor or that the editor's class name was mistyped in
    Oracle dump 分析secondary key
    java.lang.ClassNotFoundException: org.springframework.web.util.IntrospectorCleanupListener
    Oracle 验证IOT表数据存储在主键里
    Cause: java.lang.IllegalArgumentException: Mapped Statements collection does not contain value for U
    Oracle heap 表的主键 dump 分析
  • 原文地址:https://www.cnblogs.com/os-linux/p/11129315.html
Copyright © 2020-2023  润新知