字符串的方法2

# test = "郑建文妹子有种冲我来"

 一、for循环

# for 变量名 in 字符串:
# 变量名
# break
# continue


# index = 0
# while index < len(test):
# v = test[index]
# print(v)
#
# index += 1
# print('=======')

# for zjw in test:
# print(zjw)

# test = "郑建文妹子有种冲我来"
# for item in test:
# print(item)
# break         结束所有循环 
# for item in test: 
# continue 执行下一循环
# print(item)

 

二、索引,下标,获取字符串中的某一个字符

# v = test[3]
# print(v)

 

 三、切片

# v = test[0:-1] # 0=< <1
# print(v)

 

 四、获取长度

# Python3: len获取当前字符串中由几个字符组成
# v = len(test)
# print(v)

# 五、获取连续或不连续的数字,

# Python2中直接创建在内容中
# python3中只有for循环时,才一个一个创建
# r1 = range(10)
# r2 = range(1,10)
# r3 = range(1,10,2)

# 帮助创建连续的数字,通过设置步长来指定不连续
# v = range(0, 100, 5)
# for item in v:
# print(item

根据用户输入的值,输出每一个字符以及当前字符所在的索引位置 #####

# test = input(">>>")
# for item in test:
# print(item)

# 将文字 对应的索引打印出来:
# test = input(">>>")
# print(test) # test = qwe test[0] test[1]
# l = len(test) # l = 3
# print(l)
#
# r = range(0,l) # 0,3
# for item in r:
# print(item, test[item]) # 0 q,1 w,2 e

# test = input(">>>")
# for item in range(0, len(test)):
# print(item, test[item])

注意:

# 字符串一旦创建,不可修改
# 一旦修改或者拼接,都会造成重新生成字符串
# name = "zhengjianwen"
# age = "18"
# info = name + age
# print(info)

 

posted on 2018-07-12 20:32  shlvst  阅读(91)  评论(0编辑  收藏  举报

导航