標籤:函數 == this lines enter strip() new 傳回值 pac
#-*- coding:utf-8 -*-line = "l want watch movie with you ."print(line.center(50))print(line.ljust(50))print(line.rjust(50))#center 字串置中#ljust 字串居左#rjust 字元中居右#lstrip 刪除字串左邊的空白字元#rstrip 刪除字串右邊的空白字元#strip 刪除字串兩端的空白字元word = "\n \t i am a bird . \n \r "print("==lstrip==")print(word.lstrip())print("===rstrip==")print(word.rstrip())print("==strip==")print(word.strip())#空白字元包括空格 /n /t等不可見字元nword = "hello this a new world ."print("==partition==")#partitionprint(nword.partition("a"))#在源字串中從左向右尋找指定字串,找到指定字串做切割,分為3部分,指定字串前,後,其本身,傳回值是元組#如果指定字串不存在於源字串中,那麼結果由源字串本身,兩個Null 字元串組成#rpartition 從右向左尋找指定字串wline = "hello \n world"print("==splitlines==")print(wline.splitlines())#splitlines 按照行進行分割,返回一個包含各行作為元素 的列表print("==isalpha==")print(wline.isalpha()) #返回false 因為存在空格,逸出字元#isalpha 判斷字串中所有字元是否都是字母,字母返回true,其他返回falsenum = "12233"print("==isdigit==")print(num.isdigit())#isdigit 判斷字串中所有字元是否都是數字,是返回true,其他返回falsestr="sadfa213"print("==isalnum==")print(str.isalnum())#isalnum 字串是否只由數字和字母組成,是返回true,其他返回falsespace=" "print("==isspace==")print(space.isspace())#isspace 判斷字串中是否只有空格,是返回true,其他返回falseprint("==join==")tstr = "_"list1 = ["aa","bb"]print(tstr.join(list1))#每個字元後面都插入指定的字元,構成一個新的字串
Python 字串操作函數二