python四种方式读取字符串

(注意:本文基于Python3.x)

1、第一种方式,for in

girl_str = "love You"

for everyChar in girl_str:

    print(everyChar)

2、第二种方式,range()或xrange(),把字符串长度传进去就行

girl_str = "love You"

for index in range(len(girl_str)):

    print(girl_str[index])

 

3、第三种方式,enumerate()


girl_str = "love You"

for index, everyChar in enumerate(girl_str):

    print(str(index) + everyChar)

4、第四种方式,iter()


girl_str = "love You"

for everyChar in iter(girl_str):

    print(everyChar)

 

你可能感兴趣的:(python语法)