In Python, can I specify a range of string indices in a return statement placeholder?

Viewed 88

I'm practising using Python's .format method.

The first block of code below can print first_name and the first letter of last_name.

In the second block of code I was experimenting with the index in the second place holder to try and specify a slice of letters to print.

However, I can't get it to work. Is this possible?

def nametag(first_name, last_name):
    return("{} {[0]}.".format(first_name, last_name))

print(nametag("Joe", "Smith")) 
print(nametag("Frank", "Rinaldi")) 
print(nametag("Jean-Luc", "Pierre"))
def nametag(first_name, last_name):
    return("{} {[0:3]}.".format(first_name, last_name))

print(nametag("Joe", "Smith")) 
print(nametag("Frank", "Rinaldi")) 
print(nametag("Jean-Luc", "Pierre"))
0 Answers
Related