python-字串方法(40)
阿新 • • 發佈:2018-12-11
>>> py_str = 'hello word!' >>> py_str.capitalize() #把字串的第一個字元大寫 'Hello word!' >>> py_str.title() #每個單詞的第一個字元大寫 'Hello Word!' >>> py_str.center(50) #返回一個原字串居中,並使用空格填充至長度50的新字串 ' hello word! ' >>> py_str.center(50,'#') #用#填充 '###################hello word!####################' >>> py_str.ljust(50,'*') #左對齊,*填充 'hello word!***************************************' >>> py_str.rjust(50,'*') #右對齊,*填充 '***************************************hello word!' >>> py_str.count('l') #統計l出現的次數 2 >>> py_str.count('lo') 1 >>> py_str.endswith('!') #以!結尾? True >>> py_str.endswith('d!') True >>> py_str.startswith('a') #以a開頭? False >>> py_str.islower() #字母全部小寫? True >>> py_str.isupper() #字母全部是大寫? False >>> 'Hao123'.isdigit() #全部是數字? False >>> 'Hao123'.isalnum() #全部是字母數字? True >>> ' hello\t '.strip() #去除兩端的的空白 'hello' >>> ' hello\t '.lstrip() #去除左空白 'hello\t ' >>> ' hello\t '.rstrip() #去除右空白 ' hello' >>> 'how are you?'.split() #split(),分割,分片,預設以空格為分隔符 ['how', 'are', 'you?'] >>> 'hello.tar.gz'.split('.') ['hello', 'tar', 'gz'] >>> '.'.join(['hello','tar','gz']) 'hello.tar.gz' >>> '_'.join(['hello','tar','gz']) 'hello_tar_gz'