博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

py-day1-6 python 5个灰魔法 【len,index索引,for循环,切片】

Posted on 2019-02-17 12:01  MJ-majun  阅读(205)  评论(0编辑  收藏  举报
# 索引,下标,获取字符串中的某一个字符。
test = 'MuMingJun'
v = test[3]
print(v)
 
 i
# 切片
test = 'MuMingJun'
v = test[0:-1]
print(v)
x = test[0:5]
print(x)
y = test[6:9]
print(y)

MuMingJu
MuMin
Jun
# len 获取当前字符串中由几个字符组成
test = 'MuMingJun'
v = len(test)
print(v)

9
# 记住如果是有列表,者按逗号分隔计算,不按字符算
test = [12,13,14,15,16,'man','']
v = len(test)
print(v)

7
# for循环    (索引、切片都能用)                         
test = '好好学习天天向上'
for hhx in test:             for 变量名 in 字符串:
    print(hhx)                   print(变量名)
好
好
学 
习
天
天
向
上
test = '好好学习天天向上'           
for item in test:
    print(item)
    break            # 循环一次就退出
test = '好好学习天天向上'
for item in test:
    continue
    print(item)

## 无输出##

 

# index 索引
test = '好好学习天天向上'
index = 0
while index < len(test):
    v = test[index]
    print(v)
    index += 1
print('=======')

好
好
学
习
天
天
向
上
=======
# range 帮助创建连续的数字 通过设置步长来指定不连续
v = range(0,5)
x = range(0,60,20)
for item in v :
    print(item)
for tiee in x:
    print(tiee)

0
1
2
3
4  

0
20
40
#  将文字对应的索引打印出来
test = input('>>>')
for item in range(0,len(test)):
    print(item,test[item])

>>>majun
0 m
1 a
2 j
3 u
4 n