2018-12-30字符串

字符串

1,創(chuàng)建:
s1 = 'shark'
s2 = "shark"
s3 = """hello shark"""
s4 = '''hello shark'''
s5 = """hello
shark
""",
2,轉(zhuǎn)義:
testimony = 'This shirt doesn't fit me'
words = 'hello \nworld'
3(+)拼接:
print('hello' + 'world')
4()復(fù)制:
print('
' * 20)
print('shark' * 20)
字符串 和 0 或者 負(fù)數(shù)相乘,會得到一個空字符串

獲取元素

獲取單個元素
s1[0]
s1[3]
s1[-1]

1,切片技術(shù):

[start:end:step]
start:起始索引號
end:結(jié)束索引號
setp:步長
如:s1[0:2]

2,利用字符串對象的方法:

1)split
url = 'www.qfedu.com 千鋒官網(wǎng)'
url.split()
li = url.split('.')
host, *_ = url.split('.', 1)

2)rsplit 從右向左分割
url = 'www.qfedu.com'
url2 = url.rsplit('.', 1)
3)join 拼接
li = ['www', 'qfedu', 'com']
url = ''.join(li)
url2 = ''.join(li)
4)replace 替換
url = 'www.qfedu.com'
url2 = url.replace('.', '
')
5)strip 移除兩端的空格
s = ' hello '
s2 = s.strip()
6)startswith 判斷字符串以什么為開頭
s = 'hello world'
if s.startswith('h'):
print(s)
7)endswith 判斷字符串以什么為結(jié)尾
s = 'hello world'
if s.endswith('d'):
print(s)

8)index 獲取一個元素在字符串中的索引號
s = 'hello world'
idx = s.index('l')

交互輸入

inp=input("jieshou:>>")
print(inp)

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容