0

I tray to print all student names from a class with a loop and use the incremental (i) as part of the name.

I have a class with students and want to print the contents of all via a loop. Individual printing works. Another way of posing the question is how to convert a string into a variable name.

Here is an example:

class Student:
    pass
    
std1 = Student()
std2 = Student()
std3 = Student()

# student information
std1.voorn = "Boris"
std2.voorn = "Joris"
std3.voorn = "Sophie"

# printing one by one is ok for a limited number but not for large number of students
print(f"{std1.voorn}")
print(f"{std2.voorn}")
print(f"{std3.voorn}")

# I try to inject the i into the name, of course this doesn't work.
# But how to convert this to the variable naam?
for i in range(1,4):
    print(f"std{i}voorn")

0 Answers0