2021-08-29

How do I take elements from a list of n length?

I am trying to make a fun program. Its making a print statement for the famous quote: "The name is Bond, James Bond. It was really easy to make with a first name and last name, but my question now is what happens if a person has a middle name too or four names? What would the code look like? Like let us say your name Augustus De Morgan, i want the code to then give the output:

The name is de Morgan, Augustus De Morgan...

I've tried with list comprehension but can't figure it out. I must be missing out on something here.

name = input('My name is: ')
split = name.split() #makes a list

if len(split) == 2:
    print(f'The name is {split[1]}, {split[0]} {split[1]}') #picking strings from list
elif len(split) > 2:
        [print(split[i], split[0], split[i]) for i in split]


from Recent Questions - Stack Overflow https://ift.tt/3jrKi0J
https://ift.tt/eA8V8J

No comments:

Post a Comment