一、slice-切片 and index- 索引
s = "hello the cruel word"
# 01234567890123456789
# ----------|----|----
#slice stringName[start:end] == 数学[10,15)
print(s[10:15])
print(s[:5])
print(s[6:len(s)])
print(s[:])
'''
#输出
cruel
hello
the cruel word
hello the cruel word
'''
s = "hello the cruel word"
# 01234567890123456789
i = 10
st = ""
while i < 15:
st = st + s[i]
i += 1
'''
print(st) #print 语句在循环内的输出
c
cr
cru
crue
cruel
'''
print(st)
# 正常输出 cruel
切片] 依据空格作为单词分界符
s = "hello the cruel world"
i = 0
h = 0
while i < len(s):
if s[i] == " ":
v = i
print(s[h:v])
h = i + 1
i += 1
'''
输出:
hello
the
cruel
'''
s = "hellxxixixo jeapyxixedu.com"
sub = "xix"
st = ""
i = 0
while i < len(s) - len(sub) + 1:
if s[i:i + len(sub)] == sub:
print(i)
s = s[:i] +s[i + len(sub):]
i -= i + 3 #不要这行 输出 hellxixo jeapyedu.com
i += 1
print(s)
#输出 hellxixo jeapyedu.com
isspace函数来删出空格
#删出头尾的空格
#利用isspace函数来操作
h = -1
s = " hell0 jeapeu com "
i = 0
e = -1
while i < len(s):
if not s[i].isspace():
h = i
break
i += 1
print(h)
j = len(s) -1
while j >= 0:
if not s[j].isspace():
e = j
break
j -= 1
print(e)
print(s[h:e + 1])
'''
4
20
hell0 jeapeu com
'''