Python字串string的查詢和替換

2021-08-28 16:52:51 字數 652 閱讀 1667

hello_str = "hello world"

# 1. 判斷空白字元

space_str = " \t\n\r"

print(space_str.isspace())

# 2. 判斷是否以指定字串開始

print(hello_str.startswith("hello"))

# 3. 判斷是否已指定字串結束

print(hello_str.endswith("world"))

# 4. 查詢字串

print(hello_str.find("llo"))

print(hello_str.find("abc"))# find 指定字串不存在會返回-1

# 注意:index 指定字串不存在會報錯

# 5. 替換字串

# replace方法執行完成後,會返回乙個新的字串

# 注意:不會修改原有字元的內容

print(hello_str.replace("world", "python"))

print(hello_str)

輸出:

true

true

true2-1

hello python

hello world

Python基礎 String字串

usr bin env python coding utf 8 1.單雙引號轉義及字串拼接 前台介面輸入 print hello print word print hello word print hello word print hel lo name input please input pri...

Python的字串操作string

python中的字串可以使用單引號,雙引號,三引號表示。單引號 與雙引號 代表的意思相同,但要注意配對著用。並不分成單引號代表乙個字元,雙引號代表乙個字串,實際上python中沒有char。三引號 or 相比於前兩者的的優勢是,三引號內部可以自由使用單引號與雙引號,可以用於描述一段對話中 因為裡面很...

python 字串string 處理函式

字串長度獲取 len str 例 print s length d str,len str 字母處理 全部大寫 str.upper 全部小寫 str.lower 大小寫互換 str.swapcase 首字母大寫,其餘小寫 str.capitalize 首字母大寫 str.title 格式化相關 獲取...