日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python关于字符串的内置函数_Python 字符串内置函数(二)

發布時間:2024/7/19 python 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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 字符串内置函数(二)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。