Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Replace word in a list

Tags:

python

I want to show on screen all the users that live in Madrid in the following format:

The user 'NAME', lives in 'CITY' has an age of 'AGE' years old and its debt is: 'DEBT' EUR.

As you can see the different ways in which Madrid is saved, I want to find a way to print these users, since a few write in capital letters and others do not, they all live in Madrid.

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'),
    ('Elena', 35, 'MaDrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'),
    ('Carlos', 41, 'MAdrid', 580, '00887118456'),
    ('Maria', 28, 'Madrixx', 150, '587')
]

for item in students: student, age, town, debt, id = item

The desired result:

The user Marcos lives in Madrid, has an age of 23 years old and its debt is: 850 EUR.

The user Elena lives in Madrid, has an age of 35 years and its debt is: 360 EUR.

The user Carlos lives in Madrid, has an age of 41 years old and its debt is: 580 EUR.

The user Maria lives in Madrid, has an age of 28 years old and its debt is: 150 EUR.
like image 697
Eduardo Avatar asked May 13 '26 12:05

Eduardo


1 Answers

It's really hard to identify what you want, but if you are trying to print alumni data living in Madrid, try below.

students = [
    ('Marcos', 23, 'Madrid', 850, '2388711341'), 
    ('Elena', 35, 'MaDrid', 360, '0387700342'),
    ('Carmen', 21, 'Getafe', 50, '0014871388'), 
    ('Carlos', 41, 'MAdrid', 580, '00887118456'),
    ('Maria', 28, 'Madrixx', 150, '587')
]



for student_detail in students:
    if student_detail[2].lower().startswith('madri'):
        print(f"The user {student_detail[0]}, lives in Madrid. S/He is {student_detail[1]} years old and has {student_detail[3]} as debt")
like image 169
marmeladze Avatar answered May 15 '26 03:05

marmeladze