字符串 [总结]

一、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
'''



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值