[Python] How to select from index[3] to the index[8] on a list on python?

Discussão em 'Python' iniciado por Stack, Março 1, 2025.

  1. Stack

    Stack Membro Participativo

    i'm new on python and having some issues about take from a spefcific index to other. EX: i request a input of the name from every parent from the user, and want to print just the name of the brothers, but dont want to describe every index like: parent = input('Write the name of all your parents: ') print(f'The name of your brothers is {parent[1]}, {parent[2]}, etc...) i was thinking if has a way to select like print(f'... {parent[1:4]}...') PS: i'm from Brazil, so my list is in portuguese. My code actually:

    familia = input('Digite o nome da sua família! Seus pais, seus irmãos e avós: ')
    parentes = familia.split(' ')
    print(f'o seu nome é {parentes[0].capitalize()}.')
    print(f'o nome do seu pai é {parentes[1].capitalize()}.')
    print(f'o nome da sua mãe é {parentes[2].capitalize()}.')
    print(f'o nome dos seus irmãos são {parentes [3:8]}.')
    print(f'o nome dos seus avós paternos é {parentes [8:10]}.')
    print(f'o nome dos seus avós maternos é {parentes [10:12]}.')
    quantos_sao = len(parentes)
    print(f'a sua familia tem {quantos_sao} pessoas!'.capitalize())


    The 3 firts results are perfect, but when i need select more names goes like this:

    o seu nome é Gean,.
    o nome do seu pai é Enio,.
    o nome da sua mãe é Denise,.
    o nome dos seus irmãos são ['valentina,', 'anthony,', 'rafael,', 'caroline,', 'gustavo,'].
    o nome dos seus avós paternos é ['ademar,', 'gisela,'].
    o nome dos seus avós maternos é ['leonilda,', 'amazir'].
    A sua familia tem 12 pessoas!


    Someone, could help me?

    Continue reading...

Compartilhe esta Página