寫一個 Python 程序來列印字元串中的字元,並給出一個實例。
列印字元串中的字元的 Python 程序示例 1
這個 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 程序顯示字元串中的字元同上。然而,我們只是將換成了同時換成了。
# 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
原創文章,作者:小藍,如若轉載,請註明出處:https://www.506064.com/zh-tw/n/244983.html