string="i love you,you know?my name is {name},I am {year} years old "
string1="i\tlove you,you know?"
print(string.capitalize())#首字母大写
print(string.count("a",1,8))#统计a出现了多少次,如果不写后面两个,就默认第一个开始,或者最后一个结束
print(string1.center(50,"-"))#打印50个字符,包括string,不够“-”补充
print(string1.ljust(50,"-"))#打印string1,不够在右边“-”补充
print(string1.rjust(50,"-"))#打印string1,不够左边“-”补充
print(string.endswith("ow?"))#以“ow?# ”结束
print(string.startswith("i"))#以“i”开始
print(string1.expandtabs(tabsize=30))#就是把\t键转换成30个空格
print(string.find("love"))#从0开始的位置,如果是rfind()表示找到最右边相对应的位置,比如找l,但是有多个l,就找最右边的那个
print(string[string.find("love"):string.find("k")])#也可以进行切片
print(string.format(name="gongwei",year=24))#format的用法就是把{name}替换成后面的字符
print(string.format_map({"name":"gongwei","year":24 }))#用字典形式代替
print("12".isalnum())#看字符是否由数字和/或字母组成
print(string.isalpha())#看字符串是不是纯英文字符组成
print("123".isdigit())#看字符是不是纯数字
print("a_d".isidentifier())#看字符是不是一个合法的标识符/变量名
print(string.lower())#变成小写,islower()是否全是小写
print(string.upper())#变成大写,isupper()是否全是大写
print(type(",".join(['1','2','3'])))#就是把列表或者元组中的元素,通过链接制定的字符,生成新的字符串
a="--"
print(a.join(("g","o","n","g")))
print(" \nmy name is gongwei\n")#正常打印会有空格和换行(enter键)
print("\nmy name is gongwei\n".lstrip())#去除左边的\n效果和左边开头的空格
print("\nmy name is gongwei\n".strip())#去除右边边的\n效果和左边开头的空格
print("\nmy name is gongwei\n".strip())#去除所有的\n和两边的空格,注意中间的空格不会被去除
p=str.maketrans("xujing",'123456')#相当于密码本
print("gongwei".translate(p))#相当于翻译密码
print("gongwie".replace("g","G",1))#替换,默认从左开始,多少个
print("my name is gongwei".split() )#按照字符串中的空格生成一个列表,也可以自定义分割符 splitless()相当于split("\n")
print("Gong3Wei".swapcase())#大写变小写,小写变大写,数字不动
a="1+2+3+4"
print(",".join(a.split("+")))
运行结果:
I love you,you know?my name is {name},i am {year} years old
0
---------------i love you,you know?---------------
i love you,you know?------------------------------
------------------------------i love you,you know?
False
True
i love you,you know?
2
love you,you
i love you,you know?my name is gongwei,I am 24 years old
i love you,you know?my name is gongwei,I am 24 years old
True
False
True
True
i love you,you know?my name is {name},i am {year} years old
I LOVE YOU,YOU KNOW?MY NAME IS {NAME},I AM {YEAR} YEARS OLD
<class 'str'>
g--o--n--g
my name is gongwei
my name is gongwei
my name is gongwei
my name is gongwei
6o56we4
Gongwie
['my', 'name', 'is', 'gongwei']
gONG3wEI
1,2,3,4