简体   繁体   English

打印字符串Python的单个字符

[英]Printing Individual Characters of Strings Python

My code reads 我的代码读取

import time
a = raw_input("Enter a string: ")
b = len(a)
#print 1st letter of string
time.sleep(2)
#print 2nd letter of string
#continue doing that until the last character in the string

What would I need to do to make my code do that in Python 2.7? 要使我的代码在Python 2.7中做到这一点,我需要做些什么? Prods in the right direction would be much appreciated. 朝正确方向的产品将不胜感激。

Thank you. 谢谢。

You can iterate over the string itself: 您可以遍历字符串本身:

import time
a = raw_input("Enter a string: ")
for char in a:      #iterate over string one character at a time
   print char
   time.sleep(2)    # sleep after printing each character

Example: 例:

>>> for x in "foobar":
...     print x
...     
f
o
o
b
a
r
In [1]: t = 'input string'

In [2]: for c in t:
   ...:     print c
   ...:     
i
n
p
u
t

s
t
r
i
n
g

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM