Python:从字符串打印特定字符 [英] Python: print specific character from string
本文介绍了Python:从字符串打印特定字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何在 Python 中打印字符串中的特定字符?我仍在学习,现在正在尝试制作一个类似刽子手的程序.这个想法是,用户输入一个字符,如果它在单词中,则该单词会将所有未发现的字母打印为-".
How do I print a specific character from a string in Python? I am still learning and now trying to make a hangman like program. The idea is that the user enters one character, and if it is in the word, the word will be printed with all the undiscovered letters as "-".
我不是在寻求一种方法来让我的整个项目的想法/代码变得更好,只是像我所说的那样打印字符串的一个特定字符.
I am not asking for a way to make my idea/code of the whole project better, just a way to, as i said, print that one specific character of the string.
推荐答案
print(yourstring[characterposition])
示例
print("foobar"[3])
打印字母 b
mystring = "hello world"
lookingfor = "l"
for c in range(0, len(mystring)):
if mystring[c] == lookingfor:
print(str(c) + " " + mystring[c]);
输出:
2 l
3 l
9 l
更多类似刽子手:
mystring = "hello world"
lookingfor = "l"
for c in range(0, len(mystring)):
if mystring[c] == lookingfor:
print(mystring[c], end="")
elif mystring[c] == " ":
print(" ", end="")
else:
print("-", end="")
生产
--ll- ---l-
这篇关于Python:从字符串打印特定字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文