Python字符串基础
- 一、字符串运算
- 1. 字符串连接
- 2. 输出重复字符串
- 3. 通过索引输出字符串
- 4. 截取字符串
- 5. 成员运算符
- 6. 格式化字符串
- 7. 转义字符
- 二、字符串函数
- 三、字符串大小写处理
- 四、字符串填充
- 五、字符串搜索统计
- 六、字符串清洗
- 七、字符串判断
- 八、字符串切割
- 九、字符串替换
一、字符串运算
1. 字符串连接
str1="This is a "
str2="temp"
str3=str1 + str2
print (str3)
>>>This is a temp
2. 输出重复字符串
str1="good"
str2=str1*3
print(str2)
>>>goodgoodgood
3. 通过索引输出字符串
str1="ABCDEFGHIGKLMN"
str1[6:15]
>>>'GHIGKLMN'
str1[:5]
>>>'ABCDE'
str1[5:]
>>>'FGHIGKLMN'
str1[::-1]
>>>'NMLKGIHGFEDCBA'
str1[2:7:2]
>>>'CEG'
str1[-3:-6:-1]
>>>'LKG'
str1[::2]
>>>'ACEGIKM'
str1[1::2]
>>>'BDFHGLN'
4. 截取字符串
str="ABCDEFGHIGKLMN"
str[4]
>>>'E'
5. 成员运算符
'a' in 'abcd'
>>>True
'a' in 'ABCD'
>>>False
'a' not in 'ABCD'
>>>True
6. 格式化字符串
num=10
print("num=",num)
>>>num= 10
print("num=%d" %(num))
>>>num=10
print ('hello %s and %s' % ('string ', 'another string '))
>>>hello string and another string
print ('hello %(first)s and %(second)s' % {'first': 'string', 'second': 'another string'})
>>>hello string and another string
print ('hello {first} and {second}'.format(first='string', second='another string'))
>>>hello string and another string
num2=100
str='This is a temp!'
float=3.4555555
print('num2 = %d, str = %s, float = %.3f' %(num2,str,float))
>>>num2 = 100, str = This is a temp!, float = 3.456
7. 转义字符
print("This is a 'temp'!")
>>>This is a 'temp'!
print('This is a "temp"! ')
>>>This is a "temp"!
print('This is a \'temp\'! ')
>>>This is a 'temp'!
print('good\nnice\nhandsome')
>>>good
nice
handsome
print('good\\nnice\\nhandsome')
>>>good\nnice\nhandsome
print('\\\\t\\\\')
>>>\\t\\
print('\\t\\')
>>>\t\
print(r'\\t\\')
>>>\\t\\
print(R'\\t\\')
>>>\\t\\
二、字符串函数
print(eval('12+3'))
>>>15
print('12+3')
>>>12+3
print(eval('1>2'))
>>>False
print('1>2')
>>>1>2
三、字符串大小写处理
str='tHis Is a tEmp!'
str.lower()
>>>'this is a temp!'
str.upper()
>>>'THIS IS A TEMP!'
str.swapcase()
>>>'ThIS iS A TeMP!'
str.capitalize()
>>>'This is a temp!'
str.title()
>>>'This Is A Temp!'
四、字符串填充
str='This is a temp!'
str.center(40,"*")
>>>'************This is a temp!*************'
str.ljust(40,"%")
>>>'This is a temp!%%%%%%%%%%%%%%%%%%%%%%%%%'
str.rjust(40,"%")
>>>'%%%%%%%%%%%%%%%%%%%%%%%%%This is a temp!'
str.zfill(40)
>>>'0000000000000000000000000This is a temp!'
五、字符串搜索统计
len('This is a "temp"! ')
>>>18
str='This is a temp!This is a temp!'
str.count('s')
>>>4
str.find('t')
>>>10
str.find('t',11,20)
>>>-1
str.find('t',11,26)
>>>25
str.rfind('t')
>>>25
str.index('t',11,20)
>>>ValueError: substring not found
六、字符串清洗
str='***************This is a temp!***************'
str.lstrip('*')
>>>'This is a temp!***************'
str.rstrip('*')
>>>'***************This is a temp!'
str.strip('*')
>>>'This is a temp!'
七、字符串判断
- str.isalnum() 如果str至少有一个字符并且都是字母或数字则返回True,否则返回False
- str.isalpha() 如果str至少有一个字符并且都是字母则返回True,否则返回False
- str.isdigit() 如果str只包含数字则返回 True 否则返回 False
- str.islower() 如果str存在区分大小写的字符,并且都是小写则返回True 否则返回False
- str.isspace() 如果str中只包含空格,则返回 True,否则返回 False
- str.istitle() 如果str是标题化的(单词首字母大写)则返回True,否则返回False
- str.isupper() 如果str存在区分大小写的字符,并且都是大写则返回True 否则返回False
八、字符串切割
str='abc34ABC34xyz'
str.partition('34')
>>>('abc', '34', 'ABC34xyz')
str.rpartition('34')
>>>('abc34ABC', '34', 'xyz')
str.split('34')
>>>['abc', 'ABC', 'xyz']
str.split('34',1)
>>>['abc', 'ABC34xyz']
str.rsplit('34')
>>>['abc', 'ABC', 'xyz']
str.rsplit('34',1)
>>>['abc34ABC', 'xyz']
九、字符串替换
a='iamymlamleanring'
a.replace('am','*')
>>>'i*yml*leanring'
a.replace('am','*',1)
>>>'i*ymlamleanring'