字符串特性:不可修改
1、首字符大写
>>> name = "my \tname is {name} and i am {year} old"
>>> name
my name is {name} and i am {year} old
#首字符大写
>>> name.capitalize()
My name is {name} and i am {year} old
2、大写全部变小写
>>> sex = 'MAN'
>>> sex
MAN
#大写全部变小写
>>> sex.casefold()
man
3、填充
#打印50个字符,如果不够 使用“-” 补齐
>>> name.center(50,"-")
------my name is {name} and i am {year} old------
4、统计出现的次数
# 统计 a 有 几个
>>> c_str = 'java hadoop'
>>> c_str.count("a")
3
5、将字符串编码成bytes格式
>>> name
my name is {name} and i am {year} old
>>> name.encode(encoding='utf-8')
b'my \tname is {name} and i am {year} old'
6、判断字符串是否以 old 结尾
>>> name
my name is {name} and i am {year} old
#判断字符串是否以 old 结尾
>>> name.endswith("old")
true
7、将\t转换成多长的空格
>>> name = "my \tname is {name} and i am {year} old"
>>> name
my name is {name} and i am {year} old
#将tab 键转30个空格
>>> name.expandtabs(tabsize=30)
my name is {name} and i am {year} old
8、查找字符串索引,找不到返回 -1
>>> name = 'mynameisheihouzi'
>>> name.find("name")
2
9、字符串格式化 format 输出
>>> msg = "my name is {}, and age is {}"
>>> msg.format("黑猴子的家",22)
'my name is 黑猴子的家, and age is 22'
>>> msg = "my name is {1}, and age is {0}"
>>> msg.format("黑猴子的家",22)
'my name is 22, and age is 黑猴子的家'
>>> msg = "my name is {name}, and age is {age}"
>>> msg.format(age=22,name="黑猴子的家")
'my name is 黑猴子的家, and age is 22'
#format_map 传字典
>>> msg.format_map({'name':'alex','age':22})
'my name is alex, and age is 22'
10、字符串切片
#字符串也可以切片
>>> name[name.find("name"):9]
name
>>> name[name.find("name"):]
name is {name} and i am {year} old
11、返回字符串索引
>>> name = "my \tname is {name} and i am {year} old"
>>> name
my name is {name} and i am {year} old
# 返回a所在字符串的索引
>>> name.index('a')
5
12、判断是不是英文+数字
>>> 'ab23'.isalnum()
true
13、判断是不是纯英文字符
>>> 'abA'.isalpha()
true
14、判断是不是十进制
>>> '1A'.isdecimal()
false
15、判断是不是一个整数 ---常用
>>> '1A'.isdigit()
false
16、判读是不是一个合法的标识符
>>> 'a1A'.isidentifier()
true
17、判断是不是小写
>>> '33a'.islower()
true
18、判断是不是大写
>>> 'MYNAMEISHEIHOUZI '.isupper()
true
>>> 'My Name Is '.isupper()
false
19、判断是不是一个整数字
>>> '33'.isnumeric()
true
20、判断是不是title 每个首字母大写
>>> 'My Name Is '.istitle()
true
21、设备设别终端 判断能不能打印
#tty file ,drive file 设备设别终端 判断能不能打印
>>> 'My Name Is '.isprintable()
true
22、数组使用+连接起来
>>> '+'.join( ['1','2','3'])
1+2+3
23、字符串在左边,补齐字符串数量
>>> name = "my \tname is {name} and i am {year} old"
>>> name
my name is {name} and i am {year} old
#显示 50 个字符串,name放在左边,不够使用 * 补齐
>>> name.ljust(50,'*')
my name is {name} and i am {year} old************
24、字符串在右边,补齐字符串数量
>>> name = "my \tname is {name} and i am {year} old"
>>> name
my name is {name} and i am {year} old
#显示 50 个字符串,name放在右边,不够使用 - 补齐
>>> name.rjust(50,'-')
------------my name is {name} and i am {year} old
25、把大写变小写
>>> 'Hei Hou Zi De Jia'.lower()
hei hou zi de jia
26、把小写变大写
>>> 'hei hou zi de jia'.upper()
HEI HOU ZI DE JIA
27、strip() 去掉两头的空格和回车
code
print('hei hou zi de jia\n'.strip())
print("--------")
运行
hei hou zi de jia
--------
28、lstrip() 去掉左边空格和回车
code
print("-----------------")
print('\nhei hou zi de jia'.lstrip())
运行
-----------------
hei hou zi de jia
29、rstrip 去掉 右边的空格和回车
code
print('hei hou zi de jia\n'.rstrip())
print("-----------------")
运行
hei hou zi de jia
-----------------
30、1 1 对应, 密码加密 a对应3
>>> p = str.maketrans('heihouzidejia','!@#$%^&*()123')
>>> 'hei hou zi de jia'.translate(p)
'$)2 $%^ &2 () 123'
31、替换,把 h 替换成大写的 H , 只替换一个加参数 1
>>> 'hei hou zi de jia'.replace('h','H',1)
Hei hou zi de jia
32、从左边往右边数, 找到右边最后一个,返回下标
>>> 'hei hou zi ha ha'.rfind('h')
14
33、自定义,按照+号分割
>>> '1+2+3+4+5+6'.split('+')
['1', '2', '3', '4', '5', '6']
34、按照换行来分
自动帮助我们区分linux和window 换行的不同
'1+2\n+3+4'.splitlines()
['1+2', '+3+4']
35、大写转小写,小写转大写
>>> 'Hei Hou Zi de jia'.swapcase()
'hEI hOU zI DE JIA'
36、转成title 首字母大写
>>> 'hei hou zi de jia'.title()
'Hei Hou Zi De Jia'
37、用于16进制,不够的用零补齐
>>> 'hei houzi'.zfill(16)
'0000000hei houzi'