Python 字符串基础

本文详细介绍Python字符串的基础操作,包括字符串的连接、重复、索引、截取等运算,以及格式化、转义字符的使用。此外,还介绍了字符串函数、大小写处理、填充、搜索统计、清洗、判断、切割和替换等高级操作。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

一、字符串运算

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]  #从给定下标处开始截取到给定下标之前(包含6,不包含15,python从左到右下标从0开始数)
>>>'GHIGKLMN'

str1[:5]  #从头截取到给定下标之前(不含给定下标)
>>>'ABCDE'

str1[5:]  #从给定下标处开始截取到结尾(含给定下标)
>>>'FGHIGKLMN'

str1[::-1]  #逆序输出
>>>'NMLKGIHGFEDCBA'

str1[2:7:2]   #输出索引为2-6的值(CDEFG)→步长为2(CEG)
>>>'CEG'

str1[-3:-6:-1]  #输出索引为-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))    #保留3位小数点,四舍五入
>>>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\\') #Python 允许用r表示字符串中的内容都不需要转义。更多地用于windows路径中。
>>>\\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)  #不需要指定符号,默认为0
>>>'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
>>>-1

str.find('t',11,26)   #虽然该范围内总数没有25个,但是下标还是从0开始算。
>>>25

str.rfind('t')     #从左到右是10,从右到左是25
>>>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!'

七、字符串判断

  1. str.isalnum() 如果str至少有一个字符并且都是字母或数字则返回True,否则返回False
  2. str.isalpha() 如果str至少有一个字符并且都是字母则返回True,否则返回False
  3. str.isdigit() 如果str只包含数字则返回 True 否则返回 False
  4. str.islower() 如果str存在区分大小写的字符,并且都是小写则返回True 否则返回False
  5. str.isspace() 如果str中只包含空格,则返回 True,否则返回 False
  6. str.istitle() 如果str是标题化的(单词首字母大写)则返回True,否则返回False
  7. 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'
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值