1. 程式人生 > >python 基礎數據類型之字符串02

python 基礎數據類型之字符串02

方法 python 字符創

1、字符串去除空格

#
# strip(self, chars=None) #去除字符串兩端空格
# lstrip(self, chars=None) #去除字符串左端空格
# rstrip(self, chars=None) #去除字符串右端空格
程序:
str1 = "  hello world!  "
print str1.strip()
print str1.lstrip()
print str1.rstrip()
運行結果:
hello world!
hello world!  
  hello world!

2、字符串中的大小寫轉換

#
# lower(self) #全轉換為小寫
# upper(self) #全轉換為大寫
# swapcase(self) #大小寫互換
# capitalize(self) #只有字符串首字母大寫,其余都小寫
# title(self) #單詞首字母轉換為大寫
程序
str2 = "hello World!"
print str2.lower()
print str2.upper()
print str2.swapcase()
print str2.capitalize()
print str2.title()
運行結果:
hello world!
HELLO WORLD!
HELLO wORLD!
Hello world!
Hello World!

3、查找字符串位置

#
# find(self, sub, start=None, end=None) 從左邊查找字符串第一位置,找不到返回-1,找到返回索引位置
# index(self, sub, start=None, end=None) 從左邊查找字符串第一位置,找不到報錯,找到返回索引位置
# rfind(self, sub, start=None, end=None) 從右邊開始查找字符串第一位置,找不到返回-1,找到返回索引位置
# rindex(self, sub, start=None, end=None) 從右邊查找字符串第一位置,找不到報錯,找到返回索引位置
程序:
str3 = "hello world!"
print str3.find("w", 0, 3,)
print str3.index("w", 0, 7,)
print str3.rfind("l", 0, 7,)
print str3.rindex("l", 0, 7,)
運行結果:
-1
6
3
3

4、字符串對齊

#
# rjust(self, width, fillchar=None) 取固定長度右對齊,左邊不夠空格補齊
# ljust(self, width, fillchar=None) 取固定長度左對齊,右邊不夠空格補齊
# center(self, width, fillchar=None)取固定長度中間對齊,左右不夠用空格補齊
程序:
str4 = "hello world!"
print str4.rjust(20, "-")
print str4.ljust(20, "+")
print str4.center(20, "~")
運行結果:
--------hello world!
hello world!++++++++
~~~~hello world!~~~~

5、bool判斷

#
# isspace(self) 字符串是否為空格
# isupper(self) 字符串是否全大寫
# islower(self) 字符串是否全小寫
# isalnum(self) 是否全為字母或數字
# isalpha(self) 是否全字母
# isdigit(self) 是否全數字
# isspace(self) 是否是標題
# startswith(self, prefix, start=None, end=None) 是否已某字符串開頭
# endswith(self, suffix, start=None, end=None) 是否已某字符串結尾
程序:
str5 = "hello"
print str5.isspace()
print str5.islower()
print str5.startswith(" ")
print str5.endswith("!")
print str5.isalpha()
print str5.isalnum()
print str5.isalpha()
print str5.isdigit()
print str5.istitle()
運行結果:
False
True
False
False
True
True
True
False
False

6、字符串分割

#
# split(self, sep=None, maxsplit=None) 按照某符號分割,次數。
# rsplit(self, sep=None, maxsplit=None) 按照某符號從右側開始分割,次數。
# partition(self, sep: str) 字符串包含指定的分隔符,則返回一個3元的元組,第一個為分隔符左邊的子串,第二個為分隔符本身,第三個為分隔符右邊的子串。
# rpartition(self, sep: str) 類似於 partition()函數,不過是從右邊開始查找.
程序:
str6 = "www.baidu.com"
print str6.split(".")
print str6.split(".", 1)
print str6.rsplit(".", 1)
str = "http://www.baidu.//com"
print str.partition("//")
print str.rpartition("//")
運行結果:
[‘www‘, ‘baidu‘, ‘com‘]
[‘www‘, ‘baidu.com‘]
[‘www.baidu‘, ‘com‘]
(‘http:‘, ‘//‘, ‘www.baidu.//com‘)
(‘http://www.baidu.‘, ‘//‘, ‘com‘)

7、字符串連接

#
# join(self, iterable) 連接(可叠代的)元組或列表中的元素,用某字符
程序:
list1 = ("hello", "world",)
str7 = "~"
print str7.join(list1)
運行結果:
hello~world

8、字符串計數

#
# count(self, x, start, end,) 某個字符串出現的次數,開始結束位置
程序:
str8 = "hello world!"
print str8.count("l", 0, 6, )
運行結果:
2

9、tab鍵填充空格、tab用\t表示

#
# expandtabs(self, tabsize=None)把字符串中的 tab 符號(‘\t‘)轉為空格,tab 符號(‘\t‘)默認的空格數是 8
程序:
str9 = "hello\t\tworld! "
print str9.expandtabs()
運行結果:
hello           world!

10、字符串替換/格式化

#
# format(self, *args, **kwargs) 替換占位符 {0} {1} ...
# replace(self, old, new, count=None) 替換指定次數的old為new,從左開始
程序:
str10 = "hello world"
print ("name:{0} age:{1}".format("hello", "10"))
print ("hello:{name},world:{age}".format(name="yang", age="20"))
str11 = "hello hello hello!"
print str11.replace("hello", "HELLO", 2)
運行結果:
name:hello age:10
hello:yang,world:20
HELLO HELLO hello!

11、zfill()

#
# zfill(self, width: int) 方法返回指定長度的字符串,原字符串右對齊,前面填充0。
程序:
str12 = "yang"
print str12.zfill(20)
運行結果:
0000000000000000yang
#
# def decode(self, encoding: unicode = ..., errors: unicode = ...) -> unicode: ...
# def encode(self, encoding: unicode = ..., errors: unicode = ...) -> str: ...
# def splitlines(self, keepends: bool = ...) -> List[str]: ...
# def translate(self, table: Optional[AnyStr], deletechars: AnyStr = ...) -> AnyStr: ...


本文出自 “學無止境” 博客,請務必保留此出處http://20120809.blog.51cto.com/10893237/1979110

python 基礎數據類型之字符串02