编写一个 Python 程序,通过实际示例打印字符串中的字符。
Python 字符串字符打印示例程序
此 Python 程序允许用户输入一个字符串。接下来,它使用 For 循环打印字符串中的字符。在这里,我们使用 For 循环迭代字符串中的每个字符。在 Python For 循环内部,我们使用 print 语句打印字符串中的字符。
提示:请参阅 字符串 文章以了解 Python 中字符串的所有内容。
# Python program to Print Characters in a String
str1 = input("Please Enter your Own String : ")
for i in range(len(str1)):
print("The Character at %d Index Position = %c" %(i, str1[i]))

Python 字符串字符返回示例程序 2
此 Python 程序 显示字符串中的字符与上面的程序相同。但是,我们只是将 For 循环 替换为 While 循环。
# Python program to Print Characters in a String
str1 = input("Please Enter your Own String : ")
i = 0
while(i < len(str1)):
print("The Character at %d Index Position = %c" %(i, str1[i]))
i = i + 1
Python 打印字符串字符输出
Please Enter your Own String : Tutorial Gateway
The Character at 0 Index Position = T
The Character at 1 Index Position = u
The Character at 2 Index Position = t
The Character at 3 Index Position = o
The Character at 4 Index Position = r
The Character at 5 Index Position = i
The Character at 6 Index Position = a
The Character at 7 Index Position = l
The Character at 8 Index Position =
The Character at 9 Index Position = G
The Character at 10 Index Position = a
The Character at 11 Index Position = t
The Character at 12 Index Position = e
The Character at 13 Index Position = w
The Character at 14 Index Position = a
The Character at 15 Index Position = y