# __author__:Nzkalhbxx
# __date__:2017/10/15
print("count".center(37, '-'))
# count(self, sub, start=None, end=None): 从字符串的指定区间(左闭右开)内统计指定字符元素的个数
print("just do it, i believe you".count('i', 8, 21))
print("capitalize".center(37, '-'))
# capitalize(self): 字符串首字母大写
print("i love you so much. thank you".capitalize())
print("center".center(37, '-'))
# center(self, width, fillchar=None): 指定字符串长度width, 并让调用该方法的字符串居中显示, width内空余处用fillChar补充
print("我要居中".center(40, '-'))
print("endswith".center(37, '-'))
# endswith(self, suffix, start=None, end=None): 判断字符串指定区间内的字符串是否以某指定字符串结尾
print("i love you".endswith('u'))
print("i love you".endswith('lq'))
print("startswith".center(37, '-'))
# startswith(self, prefix, start=None, end=None): 判断字符串指定区间内的字符串是否以某指定字符串开始
print("i don't want to...".startswith("i don't want"))
print("expandtabs".center(37, '-'))
# expandtabs(self, tabsize=8): 设置字符串内的\t具体占多少空格
print("i lo\tve you".expandtabs(tabsize=21))
print("find".center(37, '-'))
# find(self, sub, start=None, end=None): 查找指定区间内某内容第一次出现的索引, 不存在则返回-1
print("hello world.".find('l'))
print("hello world.".find('wdq'))
print("format".center(37, '-'))
# format(*args, **kwargs), format_map(self, mapping): 字符串的格式化输出
print("my name is {name} and i am {age} years old.".format(name="wdq", age=20))
print("my name is {name} and i am {age} years old.".format_map({'name': 'psj', 'age': 18}))
print("index".center(37, '-'))
# index(self, sub, start=None, end=None): 查找指定内容在指定区间第一次在字符串中出现的索引, 不存在则报错
print("hello world.".index('l'))
# print("hello world.".index('wdq')) # ValueError: substring not found
print("isalnum".center(37, '-'))
# isalnum(self): 判断一个字符串是否只以数字和字母组成, 是则返回True, 否则返回False
print("123abc".isalnum())
print("psj".isalnum())
print("psj$abc".isalnum())
print("isdigit".center(37, '-'))
# isdigit(self): 判断字符串是否全部以数字组成, 小数也不可以
print("123".isdigit())
print("123abc".isdigit())
print("123.456".isdigit())
print("isidentifier".center(37, '-'))
# isidentifier(self): 判断一个字符串是否符合合法的变量名命名规范
print("name".isidentifier())
print("1name".isidentifier())
print("islower".center(37, '-'))
# islower(self), isupper(self): 判断字符串中包含的字母是否全部以小写/大写字符组成
print("abc".islower())
print("aBc".islower())
print("".islower())
print("123ABC".isupper())
print("aBC".isupper())
print("isspace".center(37, '-'))
# isspace(self): 判断字符串是否全部是由空格组成
print(" ".isspace())
print(" a".isspace())
print("join".center(37, '-'))
# join(self, iterable): 传入一个可迭代对象, 并将其拼接成为一个字符串
a = "i "
b = "love "
c = "you"
d = "".join([a, b, c])
print(d)
print("upper, lower".center(37, '-'))
# upper(), lower(): 将字符串中包含的字母全部变为大写/小写; swapcase(): 大小写互相转换
print("i believe you can do it.".upper())
print("I Believe You CAN DO It.".lower())
print("I Believe You Can Do It.".swapcase())
print("strip".center(37, '-'))
# strip(self, chars=None), lstrip(self, chars=None), rstrip(self, chars=None): 移除字符串两端/左端/右端的空格,Tab和换行
print(" \ti love you. \n".strip(), end="")
print("end")
print(" i love you. \n".lstrip(), end="")
print("end")
print(" i love you. \n".rstrip(), end="")
print("end")
print("replace".center(37, '-'))
# replace(self, old, new, count=None): 用指定的字符串替换原字符串中的指定的字符串指定次
print("i like Python".replace( "i", "we", 1))
print("ljust, rjust".center(37, '-'))
# ljust(self, width, fillchar=None), rjust(self, width, fillchar=None): 仅以指定字符填充左/右端
print("string center".center(40, '-'))
print("string left just".ljust(40, '-'))
print("string right just".rjust(40, '-'))
print("split".center(37, '-'))
# split(self, sep=None, maxsplit=-1): 以指定字符分割字符串指定次数, 并返回由子字符串组成的列表
print("i love you, too".split(" ", 2))
print("title".center(37, '-'))
# title(self): 以标题的形式展示字符串, 即每个单词首字母都大写
print("i am title".title())
运行结果
网友评论