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
34、字符串对齐
# # 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
False6、字符串分割
#
# 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~world8、字符串计数
#
# count(self, x, start, end,) 某个字符串出现的次数,开始结束位置
程序:
str8 = "hello world!"
print str8.count("l", 0, 6, )
运行结果:
29、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
原文地址:http://20120809.blog.51cto.com/10893237/1979110