1.首字母大写

a="alex"

v=a.capitalize()

print(v);

2.casefold将所有字符变小写,lower()也可以变小写,但是只能英文

a="aLex"

v=a.casefold()

print(v);

3.cener设置字符宽度,并将字符居中,

a="aLex"

v=a.center(20)

print(v);

结果:        aLex        

a="aLex"

v=a.center(20,"*")

print(v);

结果:********aLex********

4.count计算字符出现个数

a="aLexaLex"

v=a.count("e")

print(v);

a="aLexaLex"

v=a.count("e",5,9)

print(v);

可以设置搜索的起始和结束为止

5.以什么开始或结尾

a="aLexaLex"

v=a.endswith("x")

print(v);

结果:true

a="aLexaLex"

v=a.startswith("s")

print(v);

结果:false

6.查找

a="aLexaLex"

v=a.find("x")

print(v);

结果:3

a="aLexaLex"

v=a.find("x",5,8)

print(v);

在大于等于5并小于8的位置查找

7.format将字符串中的占位符替换为指定的值

test='i am {name}'

print(test)

v=test.format(name="alex")

print(v)

结果:

i am {name}

i am alex

test='i am {0},age {1}'

print(test)

v=test.format("alex",19)

print(v)

结果:

i am {0},age {1}

i am alex,age 19

8.字符串中是否只包含字母和数字

test="123qw_"

v= test.isalnum()

print(v)

结果:false

9.遇到tab使用几位补齐,方便打印表格

test="username\temail\tpassword\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\nlaiying\tying@q.com\t123\n"

v=test.expandtabs(20)

print(v)

结果:

username email password

laiying ying@q.com 123

laiying ying@q.com 123

laiying ying@q.com 123

laiying ying@q.com 123

10.是否全是字母

test="asdfsa"

v=test.isalpha()

print(v)

结果:true

11.值是否全是数字

test="111657"

v=test.isdigit()

print(v)

12.是否是字母数字下划线标识符

test="_111657"

v=test.isidentifier()

print(v)

13

islower是否小写

isnumeric 包含中文的数字:一 二

isprintable字符串中不包含转移符\t\n等不可见的字符

isspace是否全部是空格

istitle 是否是标题(是否首字母都是大写)

14.拆解字符串添加空格

test="你是风儿我是沙"

print(test)

t=' '

v=t.join(test)

print(v)

结果:

你是风儿我是沙

你 是 风 儿 我 是 沙

14.左右填充

test="alex"

v=test.ljust(20,"*")

print(v)

结果:alex****************

rjust靠右

15.
islower是否小写
lower转为小写
isupper是否是大写
upper转为大写
16.去掉空白或指定字符
lstrip去掉左侧空格,\t\n
rstrip去掉右侧空格,\t\n

v=test.lstrip('9ah')将左侧的9ah 去掉,没有则去掉9ah其中任何一个

17.做对应关系,然后替换

maketrans

18.分割
test="alex"
v=test.partition("e")
print(v)
结果:('al', 'e', 'x')
split
19.splitlines根据换行符分割
20.大小写取反
test="alEx"
v=test.swapcase()
print(v)
结果:
ALeX
21.可以使用下标取字符串中的某一个字符
test="alex"
v=test[0]

v=test[0:1]取大于等于0小于1 的字符
22.获取长度时,如果是list,结果是元素个数,而不是字符总数

li=[11,22,33,44,'asdf']
v=len(li)

print(v)

结果:5

 

23.使用for循环打印字符串的每个字符

test="中华人民"

for zjw in test:

print(zjw)

结果:

24.字符串在内存中一旦创建就不可修改,如果要修改,需要重新创建一个字符串

25.替换replace

26.创建连续的数字

v=range(100)

test=input(">>>")
for item in range(0,len(test)):
print(item,test[item])
结果:

>>>asdf
0 a
1 s
2 d
3 f