设为首页 加入收藏

TOP

Python基础——字符串操作
2019-03-15 12:08:01 】 浏览:55
Tags: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

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇Python入门教程三:显示'Welc.. 下一篇Python爬虫7-Cookie & Session

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目