I have a string str="vivian_wang" and I have a list variable lst = [1,4]. How can I print the letter in the string against my variable lst as index.
Asked
Active
Viewed 39 times
-1
-
2Anything wrong with `print([str[index] for index in lst])`? – BrokenBenchmark Feb 15 '22 at 17:55
-
2Also, don't use `str` as a variable name, as it [shadows builtins](https://stackoverflow.com/questions/9109333/is-it-bad-practice-to-use-a-built-in-function-name-as-an-attribute-or-method-ide). – BrokenBenchmark Feb 15 '22 at 17:57
-
1[How to ask homework question](https://meta.stackoverflow.com/questions/334822/how-do-i-ask-and-answer-homework-questions) and [Open letter to students with homework problems](https://softwareengineering.meta.stackexchange.com/questions/6166/open-letter-to-students-with-homework-problems) – Barmar Feb 15 '22 at 18:09
1 Answers
0
so I'm guessing in this example, you'd want to print out str[1] and str[4] — based on the values in 'lst'
for item in lst:
print(str[item])

capcode
- 1
- 2