Python 字符串內置函數(二)

# 2.格式化相關# ljust(width)  函數   獲取固定長度,左對齊,右邊不夠用空格補齊# rjust(width)  函數   獲取固定長度,右對齊,左邊不夠用空格補齊# center(width) 函數   獲取固定長度,中間對齊,兩邊不夠用空格補齊# zfill(width)  函數   獲取固定長度,右對齊,左邊不足用0補齊# format()      函數   字符串格式化的功能# endswith()    函數   用於判斷字符串是否以指定後綴結尾,若是以指定後綴結尾返回True,不然返回False。可選參數"start"與"end"爲檢索字符串的開始與結束位置。# startswith()  函數   用於檢查字符串是不是以指定子字符串開頭,若是是則返回 True,不然返回 False。若是參數 beg 和 end 指定值,則在指定範圍內檢查。a='1 2'print(a.ljust(10))print(a.rjust(10))print(a.center(10))print(a.zfill(10))'''執行結果:1 2              1 2   1 2    00000001 2'''# format()字符串格式化的功能print("{1} {0} {1}".format("hello", "world"))  # 設置指定位置# 結果:'world hello world'print("網站名:{name}, 地址 {url}".format(name="菜鳥教程", url="www.runoob.com"))# 結果:網站名:菜鳥教程, 地址 www.runoob.com# endswith()函數用於判斷字符串是否以指定後綴結尾,若是以指定後綴結尾返回True,不然返回False。可選參數"start"與"end"爲檢索字符串的開始與結束位置。str = "this is string example....wow!!!";suffix = "wow!!!";print(str.endswith(suffix)) # 結果:Trueprint(str.endswith(suffix, 20)) # 結果:Truesuffix = "is";print(str.endswith(suffix, 2, 4)) # 結果:Trueprint(str.endswith(suffix, 2, 6)) # 結果:False# startswith()函數用於檢查字符串是不是以指定子字符串開頭,若是是則返回 True,不然返回 False。若是參數 beg 和 end 指定值,則在指定範圍內檢查。str = "this is string example....wow!!!";print(str.startswith( 'this' )) # 結果:Trueprint(str.startswith( 'is', 2, 4 )) # 結果:Trueprint(str.startswith( 'this', 2, 4 )) # 結果:False
相關文章
相關標籤/搜索