python关于字符串的内置函数_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)) # 結果:True
print(str.endswith(suffix, 20)) # 結果:True
suffix = "is";
print(str.endswith(suffix, 2, 4)) # 結果:True
print(str.endswith(suffix, 2, 6)) # 結果:False
# startswith()函數用于檢查字符串是否是以指定子字符串開頭,如果是則返回 True,否則返回 False。如果參數 beg 和 end 指定值,則在指定范圍內檢查。
str = "this is string example....wow!!!";
print(str.startswith( 'this' )) # 結果:True
print(str.startswith( 'is', 2, 4 )) # 結果:True
print(str.startswith( 'this', 2, 4 )) # 結果:False
總結
以上是生活随笔為你收集整理的python关于字符串的内置函数_Python 字符串内置函数(二)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 怎么把php的时区配置为本地,PHP本地
- 下一篇: java 实现打印条形码_条码打印软件中