1.0 双引号和单引号的区别
a = "Let's go" print(a) #>>>Let's go
2.0 重复输出字符串*
print('hello'*2) #>>>hellohello
3.0 字符串索引
print('helloworld' [2:]) #>>>lloworld
4.0 判断in
print(123 in [23,45,123]) #>>>True
5.0 格式化字符串 %
print('%s is a good teacher'%'alex') #>>>alex is a good teacher
6.0 字符串拼接+
a = '123' b = 'abc' c = a + b print(c) #>>>123abc 效率低,需要不停开辟内存空间
7.0 字符串拼接(此方法较好)
a = '123' b = 'abc' c = ''.join([a,b]) print(c) #>>>123abc
7.1 拼接 ''.join()
a = '123' b = 'abc' d = 'miaomiao' c = '------'.join([a,b,d]) print(c) #>>>123------abc------miaomiao
8.0 字符串的内置方法
8.1 count(计算字符串的个数)
st = 'hello kitty' print(st.count('l')) #统计元素个数 #>>>2
8.2 capitalize(将首字母大写)
st = 'hello kitty' print(st.capitalize()) #首字母大写
8.3 center
st = 'hello kitty' print(st.center(50,'-')) #>>>-------------------hello kitty--------------------
8.4 endswith(判断是否以某个字符串结尾)
st = 'hello kitty' print(st.endswith('ty')) #>>>True
8.5 startwith(判断是否以某个字符串开头)
st = 'hello kitty' print(st.startswith('he')) #>>>True
8.6 expandtabs(可以把tab转换成XX个空格)
st = 'he llo kitty' print(st.expandtabs(tabsize=10)) #>>>he llo kitty
8.7 find(查找到第一个元素,并将索引值返回)
st = 'hello kitty' print(st.find('t')) #>>>8
8.8 format()
st = 'hello kitty{name} is {age}' print(st.format(name='alex',age=37)) print(st.format_map({'name':'alex','age':22})) #>>>hello kittyalex is 37 #>>>hello kittyalex is 22
8.9 index
st = 'hello kitty' print(st.index('t')) #>>>8
8.10 isalnum(查看是否是字符串,如果是特殊符号的话就为False)
st = 'abc345' sr = '#123' print(st.isalnum()) #>>>True print(sr.isalnum()) #>>>False
8.11 isdigit(判断是否是整型数字)
print('1234'.isdigit()) #>>>True
print('1234.123'.isdigit()) #>>>False
8.12 isnumeric(判断是否是数字,和isdigit一样)
print('1234'.isnumeric()) #>>>True
8.13 isidentifier(判断变量名是否规范)
print('34abc'.isidentifier()) #>>>False
8.14 islower(判断是否全是小写)
print('abc'.islower()) #>>>True
8.15 isupper(判断是否全是大写)
print('ABC'.isupper()) #>>>True
8.16 isspace(判断是否是空格)
print(' '.isspace()) #>>>True
8.17 istitle(判断是否首字母全都是大写)
print('My Title'.istitle()) #>>>True
8.18 lower(所有字母全都变小写)
print('My Title'.lower()) #>>>my title
8.19 upper(所有字母全都变成大写)
print('My Title'.upper()) #>>>MY TITLE
8.12 swapcase(大写变成小写,小写变成大写)
print('My Title'.swapcase()) #>>>mY tITLE
8.13 ljust 和 rjust
print('My Title'.ljust(50,'*')) #>>>My Title****************************************** print('My Title'.rjust(50,'*')) #>>>******************************************My Title
8.14 strip(去掉前面的空格)
print(' My Title '.strip()) #>>>My Title
8.14 lstrip
print(' My Title '.lstrip()) #>>>My Title #>>> #>>>
8.15 rstrip
print(' My Title '.rstrip()) #>>> My Title
8.16 replace
print('My Title'.replace('Title','lesson')) #>>>My lesson
8.16.2
print('My Title Title'.replace('itle','lesson',1)) #>>>My Tlesson Title
8.17 rfind
print('My Title Title'.rfind('T')) #>>>9
8.18 split(以XX分割)
print('My Title Title'.split(' ')) #>>>['My', 'Title', 'Title']
print('My Title Title'.split('i')) #>>>['My T', 'tle T', 'tle']
8.19 rsplit(只分割一次,以右为准)
print('My Title Title'.rsplit('i',1)) #>>>['My Title T', 'tle']