python基礎 字串常用函式

2021-08-28 07:37:18 字數 666 閱讀 6096

1.startswith判斷字串是否以某個字元或字串開頭

#! /usr/bin/env python

# -*- coding: utf-8 -*-

str1 = "sdssd"

#判斷str1是否以sds開頭,結果返回true

print str1.startswith("sds")

#判斷str1是否以s開頭,結果返回true

print str1.startswith("s")

#判斷str1是否以e開頭,結果返回false

print str1.startswith("e")

2.endswith判斷字串是否以某個字元或字串開頭

#! /usr/bin/env python

# -*- coding: utf-8 -*-

str1 = "sdssd"

#判斷str1是否以sds結尾,結果返回true

print str1.endswith("sd")

#判斷str1是否以d結尾,結果返回true

print str1.endswith("d")

#判斷str1是否以e結尾,結果返回false

print str1.endswith("e")

PYTHON字串常用函式

1.find and rfind 從左開始找 title find le 存在返回索引值,不存在 1 從右開始找 title find le 存在返回索引值,不存在 1 2.join 列表轉成字串 join list 3.split 字串轉成列表 ss,aa,cc split ss aa cc 4....

Python字串常用函式

capitalize 把字串的第乙個字元改為大寫 casefold 把整個字串的所有字元改為小寫 center width 將字串居中,並使用空格填充至長度width的新字串 count sub start end 返回sub在字串裡邊出現的次數,start和end引數表示範圍,可選。encode ...

Python 字串常用函式

函式 描述 返回值 str.capitalize 將字串的第乙個字元大寫 str.title 返回標題化的字串,即每個單詞的首字母都大寫 str.upper 全大寫str.lower 全小寫len str 返回字串的長度。用法與其他不同。str.count substring start end 統...