EVA*_*ATE 3 python printing string
如何在Python中从字符串中打印特定字符?我仍在学习,现在尝试制作类似hangman的程序。这个想法是用户输入一个字符,如果它在单词中,则该单词将以所有未发现的字母打印为“-”。
我并不是在寻求一种使我的整个项目的想法/代码更好的方法,而只是要求一种打印字符串的特定字符的方法。
print(yourstring[characterposition])
Run Code Online (Sandbox Code Playgroud)
例
print("foobar"[3])
Run Code Online (Sandbox Code Playgroud)
打印字母 b
编辑:
mystring = "hello world"
lookingfor = "l"
for c in range(0, len(mystring)):
if mystring[c] == lookingfor:
print(str(c) + " " + mystring[c]);
Run Code Online (Sandbox Code Playgroud)
输出:
2 l
3 l
9 l
Run Code Online (Sandbox Code Playgroud)
还有更多关于of子手的说法:
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="")
Run Code Online (Sandbox Code Playgroud)
产生
--ll- ---l-
Run Code Online (Sandbox Code Playgroud)