Python基础——字符串操作
运算符
加(+)
str2="hello"+"python" print(str2)
乘(*)
str1="hello python" str1*3
长度
str2="hello"+"python" print(str2)
len(str2)
切分
strSplit="1 2 3 4 5" strSplit.split()
strSplit="1,2,3,4,5" strSplit=strSplit.split(",") strSplit
结合
str3='1,2,3,4,5' strJoin='' strJoin.join(str3)
替换
strReplace="hello python" strReplace=strReplace.replace("python","world") strReplace
大小写转换
strReplace='hello world' strReplace.upper()
strReplace='HELLO WORLD' strReplace.lower()
去掉空格
去掉前后空格
strStrip=' hello python ' strStrip.strip()
去掉左边空格
strStrip.lstrip()
去掉右边空格
strStrip.rstrip()
字符串格式
'{} {}'.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
判断字符是否在字符串中
result='hello python 123.000000 456' 'hello' in result
'he' in result
'123' in result
' ' in result