字符串的各种方法

str.strip(a) #默认移除左右空格、换行、Table键,或者移除a

str.capitalize() #首字母大写,其他改成小写

str.casefold() #全部转化成小写

str.lower() #转化成小写

str.replace("a","A",cont) #将字符串中原来的a,替换为A,也可以自己指定替换次数cont。

str.center(length,"") #占位length,居中显示,不够默认填充空格,或者指定输入

str.count("a",1,5) #从字符串的1到5位置检索a的数量,返回数量

str.endwith("a") #是否以a结尾

str.expandtabs(int) #设置字符串中table键的空格长数。

str.find("a") #查找字符串中的a,找到返回第一个索引,找不到返回-1

str.format() #格式化输出

1、"my name is {0},i am  {1} years old!".format("G-YUE",22)

    my name is G-YUE,i am 22 years old!

2、name="G-YUE" age=22
   a={"name":"G-YUE","age":22}

    ""my name is {name},i am  {age} years old!".format(name,age)"

    my name is G-YUE,i am 22 years old!

    ""my name is {name},i am  {age} years old!".format(a)"

    my name is G-YUE,i am 22 years old!

str.isalnum() #判断字符串是否是仅含有数字字母组成的。

str.isdecimal() #判断字符串中是否是十进制正整数

str.isalpha() #判断字符串是不是字母

str.isidentifier() #判断字符串可不可以作为合法的变量名

str.islower() #判断是不是小写

str.isupper() #判断是不是大写

str.isnumeric() #判断是不是数字

str.isspace() #判断是不是空格

str.istitle() #是不是英文标题,每个单词首字母都大写

str.join(a) #以str作为连接符,把a输入的值拼接成字符串

str.ljust() #左对齐

str.rjust() #右对齐

str.swapcase() #大小写互换

str.translate(IN,OUT) #字符翻译,IN="abc"
OUT="123" 把字符串中对应的abc,改成123

原文地址:https://www.cnblogs.com/G-YUE/p/6653393.html