python关于字符串的内置函数_Python 字符串内置函数(二)
# 2.格式化相關(guān)
# ljust(width) 函數(shù) 獲取固定長度,左對齊,右邊不夠用空格補齊
# rjust(width) 函數(shù) 獲取固定長度,右對齊,左邊不夠用空格補齊
# center(width) 函數(shù) 獲取固定長度,中間對齊,兩邊不夠用空格補齊
# zfill(width) 函數(shù) 獲取固定長度,右對齊,左邊不足用0補齊
# format() 函數(shù) 字符串格式化的功能
# endswith() 函數(shù) 用于判斷字符串是否以指定后綴結(jié)尾,如果以指定后綴結(jié)尾返回True,否則返回False。可選參數(shù)"start"與"end"為檢索字符串的開始與結(jié)束位置。
# startswith() 函數(shù) 用于檢查字符串是否是以指定子字符串開頭,如果是則返回 True,否則返回 False。如果參數(shù) beg 和 end 指定值,則在指定范圍內(nèi)檢查。
a='1 2'
print(a.ljust(10))
print(a.rjust(10))
print(a.center(10))
print(a.zfill(10))
'''
執(zhí)行結(jié)果:
1 2
1 2
1 2
00000001 2
'''
# format()字符串格式化的功能
print("{1} {0} {1}".format("hello", "world")) # 設(shè)置指定位置
# 結(jié)果:'world hello world'
print("網(wǎng)站名:{name}, 地址 {url}".format(name="菜鳥教程", url="www.runoob.com"))
# 結(jié)果:網(wǎng)站名:菜鳥教程, 地址 www.runoob.com
# endswith()函數(shù)用于判斷字符串是否以指定后綴結(jié)尾,如果以指定后綴結(jié)尾返回True,否則返回False。可選參數(shù)"start"與"end"為檢索字符串的開始與結(jié)束位置。
str = "this is string example....wow!!!";
suffix = "wow!!!";
print(str.endswith(suffix)) # 結(jié)果:True
print(str.endswith(suffix, 20)) # 結(jié)果:True
suffix = "is";
print(str.endswith(suffix, 2, 4)) # 結(jié)果:True
print(str.endswith(suffix, 2, 6)) # 結(jié)果:False
# startswith()函數(shù)用于檢查字符串是否是以指定子字符串開頭,如果是則返回 True,否則返回 False。如果參數(shù) beg 和 end 指定值,則在指定范圍內(nèi)檢查。
str = "this is string example....wow!!!";
print(str.startswith( 'this' )) # 結(jié)果:True
print(str.startswith( 'is', 2, 4 )) # 結(jié)果:True
print(str.startswith( 'this', 2, 4 )) # 結(jié)果:False
總結(jié)
以上是生活随笔為你收集整理的python关于字符串的内置函数_Python 字符串内置函数(二)的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么把php的时区配置为本地,PHP本地
- 下一篇: python logging模块的作用及