字符串
1,創建:
s1 = 'shark'
s2 = "shark"
s3 = """hello shark"""
s4 = '''hello shark'''
s5 = """hello
shark
""",
2,轉義:
testimony = 'This shirt doesn't fit me'
words = 'hello \nworld'
3(+)拼接:
print('hello' + 'world')
4()復制:
print('' * 20)
print('shark' * 20)
字符串 和 0 或者 負數相乘,會得到一個空字符串
獲取元素
獲取單個元素
s1[0]
s1[3]
s1[-1]
1,切片技術:
[start:end:step]
start:起始索引號
end:結束索引號
setp:步長
如:s1[0:2]
2,利用字符串對象的方法:
1)split
url = 'www.qfedu.com 千鋒官網'
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 判斷字符串以什么為結尾
s = 'hello world'
if s.endswith('d'):
print(s)
8)index 獲取一個元素在字符串中的索引號
s = 'hello world'
idx = s.index('l')
交互輸入
inp=input("jieshou:>>")
print(inp)