突发奇想

《突发奇想》
#   编写for循环,利用索引遍历出每个字符
#   方法1
msg = 'hello script_child 666'
msg_len = len(msg)
for i in range(msg_len):
    print(msg[i])


#  方法2
msg = 'hello script_child'
count = 0
while True:
    print(msg[count])
    count += 1
    if count == len(msg):
        break


#  方法3
msg = 'hello ndy'
msg_new = msg.replace('ndy','script_child')
print(msg_new)


#   方法4
s1 = ''
s2 = ('h','e','l','l','o','s','c','r','i','p','t','c','h','i','l','d')
s3 =s1.join(s2)
print(s3)


#  方法5
s1 = 'hello,script_child'
s2 = s1.zfill(0)
print(s2)


#  方法6
s1 = '{},{}'
s2 = s1.format('hello','script_child')
print(s2)


#  方法7
s1 = '{1},{0}'
s2 = s1.format('script_child','hello')
print(s2)


#方法8                         ########
s1 = "{name1}, {name2}"
s2 = s1.format(name2 = 'script_child',name1 = 'hello')
print('s2')

s = "I am dark {name},I'm {age} years old!"
s1 = s.format(age = '28',name = 'script_child')
print(s1)


#  方法9
s1 = 'hello script_child'
s2 = s1.encode('utf-8')
print(s2)
s3 = s2.decode('utf-8')
print(s3)


#  方法10
s1 = ['hello script_child']
s2 = []
s3 = s1 + s2
print(s3)


#  方法11            #
list = ['hello script_child']
for i in range(len(list)):
    print(list[i])


#   方法12            # for语句和enumerate()函数遍历列表
list = ['hello script_child']
for index,value in enumerate(list):
    print("%d【%s】" % (index, value))

# print("第%d个元素值是【%s】"%(index,value))


#   方法13            #  列表排序,升序
list = ['hello script_child']
list.sort()
print(list)
        #             #  列表排序,降序
list = ['hello script_child']
list.reverse()
print(list)
        #             #  对列表先进行正序再反序
list = ['script_child','hello']
list.sort()
list.reverse()
print(list)
                      # 对列表先进行反序再正序
list = ['script_child','hello']
list.reverse()
list.sort()
print(list)


#  方法14               #定义多维列表
list = [['hello','script_child']]
for i in range(len(list)):
    list2 = list[i]
    for j in range(len(list2)):
        print(list2[j])


#  方法15
s1 = ('hello','script_child')
print(s1[0])
print(s1[1])


#  方法16
s1 =set(['hello scriptchild'])
s1.update([])
print(s1)


#  方法17
d = {'g':'hello scriptchild'}
print(d['g'])


d = {'name':'xiaoming','age':'19'} #访问字典元素的例子
print(d['name'])
print(d['age'])


d = {'name':'xiaoming'}  # 添加字典元素  ,
d ['age'] = '19'
print(d)

  


  注:只是一些偶然的突发奇想,并没有很大的专业程度

posted @ 2018-01-25 12:28  脚本小孩  阅读(272)  评论(0)    收藏  举报