python

python 基本数据类型二

2018-04-28  本文已影响0人  little_short

字符串魔法
-expandtabs() 断句 分隔的值
test = 'username\temail\tpassword\nsss\tbaidu.com\t'
v = test.expandtabs(20)
print(v,len(v))
-isalpha() 是否全是字母 汉子
test = 'asdf'
v = test.isalpha()
print(v)
-isdecimal() isdigit() 判断是否是数字 第二个可判断更多 isnumeric() 可判断中文
test = '二'
v1 = test.isdecimal()
v2 = test.isdigit()
v3 = test.isnumeric()
print (v1,v2,v3)
-isidentifier() 数字,字母,下划线: 标识符 def class 判断是否是合法的 标识符
a = "2"
v = a.isidentifier()
print(v)
-isprintable() 可以打印并且是真实值 是否包含不可显示的字符
test = 'exportss\t'
v = test.isprintable()
print(v)
-isspace() 判断是否全是空格
test = 'sdad sd'
v = test.isspace()
print (v)
-title() 变成标题 -istitle() 判断是否是标题
test = 'run fast more'
v1 = test.istitle()
v = test.title()
v2 = v.istitle()
print(v)
print (v1)
print(v2)
-join 字符串按照指定分隔符拼接
test = '你是风儿,我是沙'
t = '
'
v = t.join(test)
print(v)
-ljust 左填充 -rjust 又填充
test = 'saddasd'
v = test.ljust(20,'*')
print (v)

    v1 = test.rjust(20,'*')
    print (v1)
-islower() 是否都是小写 -lower()转换小写  -isupper() 是否都是大写  -upper() 转换大写
    test = 'Asss'
    v2 = test.lower()
    v1 = v2.islower()
    print (v1,v2)
    
    test = 'Asss'
    v2 = test.upper()
    v1 = v2.isupper()
    print (v1,v2)
-lstrip() -rstrip()  -strip()  移除指定字符,\n、\t、 和匹配的字符
    test = 'sdvvvsd'
    v1 = test.lstrip('sd')
    v2 = test.rstrip('sd')
    v3 = test.strip('sd')
    print (v1)
    print (v2)
    print (v3)
-maketrans()  -translate() 对应的替换
    test = 'aeiou'
    test1 = '12345'
    v = 'aaaaaeeeeeissggioo'
    m = str.maketrans(test,test1)
    new_v = v.translate(m)
    print(new_v)
-partition() -rpartition() 分隔但是不去掉分隔的字符  -split()  -rsplit()   分隔但是去掉分隔的字符
    test = 'testssss'
    v1 = test.partition('s')
    v2 = test.rpartition('s')
    v4 = test.split('s',1)
    v6 = test.rsplit('s',1)
    print(v1)
    print (v2)
    print(v4)
    print (v6)
-splitlines() 根据换行分隔  True False 是否保留换行
    test = 'asda\ndsdasd\nsdasd'      
    v = test.splitlines(True)         
    print(v) 
-startswith()以什么开始     -endswith() 以什么结尾
    test = 'backend 1.1.1.1'      
    v = test.startswith('ba')     
    print(v)                      
    v1 = test.endswith('ba')      
    print(v1)    
-swapcase()  大小写互换
    test = 'DDDsdaad'           
    v = test.swapcase()         
    print(v)  
-replace() 替换
    test = 'aaaacccc'
    v = test.replace('a','bbv',1)
    print(v)        
                                  
7个基本魔法
    join
    split
    find
    strip
    upper
    lower
    replace
灰魔法  

-索引下标  获取字符串中的某一个字符 切片
    test = 'sdadas'
    v = test[0]    
    v1 = test[0:2] 
    v2 = test[0:-1]
    print(v)       
    print(v1)      
    print(v2)     
-len 
    p2 中文长度 不是3 是9
    test = 'sdadas'        
    test1 = '撒大大'          
    v = len(test)          
    v1 = len(test1)        
    print(v,v1)    
-join 

-for
    test = '去年买了个大书包'
    # index = 0
    # while index < len(test):
    #     v = test[index]
    #     index+=1
    #     print(v)
    # print('=====')

    for qnm in test:
        continue
        print (qnm)
        break
-range() 创建数字  同步步长创建不连续数字
    v = range(0,100,5)
    print(v)
    for item in v:
        print(item)

深灰魔法
-字符串一旦创建 便不可修改(内存中)
-字符串修改实际是 新生成一个字符串

上一篇下一篇

猜你喜欢

热点阅读