zoukankan      html  css  js  c++  java
  • python之字符串 元祖 列表 字典

    一 字符串操作

    语法:' '

    类型:str

    #首字母大写其余全部小写
    test1 = 'yanShichenG'
    v = test1.capitalize() 
    #全部小写(可以处理特殊字符)
    v1 =test1.casefold()  
    #全部小写(只能处理英文)
    v2 = test1.lower()
    #大写转小写小写转大写
    v = test.swapcase()
    #字符串转小写
    v2 = test.lower()
    #转大写
    v2 = test.upper()
    
    #格式化
    #设置宽度将内容居中,默认空格填充
    v2 = test1.center(20,'-')
    #左边格式化
    v3 = test1.ljust(20,'-')
    #右边格式化
    v4 = test1.rjust(20,'-') 
    #0填充左边
    v5 = test1.zfill(20) 
    #字符串格式化 拼接 (类似于linux传参)
    test2 = "I am {name},age {a}"
    a1 = test2.format(name='yanshicheng',a=20) 
    #字符串拼接 传入的值是一个字典
    a2 = test2.format_map({"name":'yanshciheng',"a":22})
    #每隔20个断句 	自动填充到20 可以制作表格
    test5 = "Username	email	password
    yanshicheng	oscloud@sina.com	123456
    yanshicheng	oscloud@sina.com	123456
    yanshicheng	oscloud@sina.com	123456
    "
    x = test5.expandtabs(20)
    print(x)
    #根据指定的某个分隔符将字符串元素拼接
    test = "你是风儿我是沙"
    t = ' '
    v = t.join(test)
    v1 = "_".join(test)
    #根据换行符分割默认 false不输出换行符 v1 = test.splitlines(true)则打印换行符
    v1 = test.splitlines()
    
    #查找n字符出现的次数默认从开始到结束,可指定查找范围v3 = test1.count('n',5,20)    
    #判断字符串以什么结尾 真==True 假==false
    v4 = test1.endswith('a')
    #判断字符串以什么开头 真==True 假==false
    v5 = test1.startswith('y')
    #判断字符串中只包含数字或者字符为真
    v =test.isalnum()
    #判断字符串只能包含中文
     v = test.isalpha()
    #判断字符串是不是数字
     v = test.isalpha()
    #判断数字 可以判定特殊字符如:② 不能判定中文字符如:二
     v = test.isalpha()
    #可以判定特殊字数字和中文数字
    v3 = test.isnumeric()
    #可以判断字母,数字,下划线:标识符, def 
    v1 = test1.isidentifier()
    #所有值打印都可显示为True false例如
     	
    v = test.isprintable()
    #判断全部为空格为真
    v = test.isspace()
    #判断首字母是否为大写
    v1 = test.title()
    #判断是否是一个标题(所有首字母大写)
    v2 = v1.istitle()
    #判断是否全部是小写
    v1 = test.islower()
    #判断全部是大写
    v2 = test.lower()
    #判断是否以b开头
    v1 = test.startswith('b')
    #判断以某个字符结尾
    # v2 = test.endswith('z') 
    
    #查找下标从前往后找可指定位置
    a = test.find('an')
    #查找下标找不到报错
    b = test.index('n')  
    #如果左边有空格只打印左边
    v1 = test.lstrip()
    #如果两边有空格只打印右边
    v2 = test.rstrip()
    #不打印空格
    v3 = test.strip() 
    
    #取出左边指定的字符不可以有空格
    test = "yanshicheng"
    v4 = test.lstrip('9y')   #anshicheng
    #匹配到去除
    v5 = test.strip('yang')
    
    #找到第一个参数进行分割
    test = "yanshishicheg"
    v1 = test.partition('s')   #('yan', 's', 'hishicheg')
    #在后面找到第一个参数进行分割
    v2  = test.rpartition('s') 
    #默认全部分割 可指定分割次数
    v3 = test.split('s',1)
  • 相关阅读:
    HDU 1022 Train Problem I
    HDU 1702 ACboy needs your help again!
    HDU 1294 Rooted Trees Problem
    HDU 1027 Ignatius and the Princess II
    HDU 3398 String
    HDU 1709 The Balance
    HDU 2152 Fruit
    HDU 1398 Square Coins
    HDU 3571 N-dimensional Sphere
    HDU 2451 Simple Addition Expression
  • 原文地址:https://www.cnblogs.com/yanshicheng/p/9508781.html
Copyright © 2011-2022 走看看