运算符

加(+)

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。  
str2="hello"+"python"
print(str2)

Python基础——字符串操作 Python 第1张

乘(*)

 
str1="hello python"
str1*3

Python基础——字符串操作 Python 第2张

   

Python基础——字符串操作 Python 第3张

长度

str2="hello"+"python"
print(str2)
len(str2)

Python基础——字符串操作 Python 第4张

Python基础——字符串操作 Python 第5张

切分

strSplit="1 2 3 4 5"
strSplit.split()
strSplit="1,2,3,4,5"
strSplit=strSplit.split(",") 
strSplit

Python基础——字符串操作 Python 第6张

结合

str3='1,2,3,4,5'
strJoin=''
strJoin.join(str3)

Python基础——字符串操作 Python 第7张

替换

strReplace="hello python"
strReplace=strReplace.replace("python","world")
strReplace

Python基础——字符串操作 Python 第8张

大小写转换

strReplace='hello world'
strReplace.upper()
strReplace='HELLO WORLD'
strReplace.lower()

Python基础——字符串操作 Python 第9张

Python基础——字符串操作 Python 第10张

去掉空格

去掉前后空格

strStrip='     hello python    '
strStrip.strip()

Python基础——字符串操作 Python 第11张

去掉左边空格

 
strStrip.lstrip()

Python基础——字符串操作 Python 第12张

去掉右边空格

 
strStrip.rstrip()

Python基础——字符串操作 Python 第13张

字符串格式

'{} {}'.format('hello','python')
'{1} {0}'.format('hello','python')
'{hello} {python}'.format(hello=10,python=5)
strFormat="hello python"
a=123.0
b=456
result='%s %f %d'%(strFormat,a,b)
result
 

Python基础——字符串操作 Python 第14张

判断字符是否在字符串中

result='hello python 123.000000 456'
'hello' in result
'he' in result
'123' in result
' ' in result

Python基础——字符串操作 Python 第15张

Python基础——字符串操作 Python 第16张

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄