how shall i call out this function using string format method?

Question:

i’m learning Python and have began with Google’s Python Automation Beginner course. Idk if i chose it right but im already in week 4 and now have started facing confusion.

Fill in the gaps in the nametag function so that it uses the format method to return first_name and the first initial of last_name followed by a period. For example, nametag(“Jane”, “Smith”) should return “Jane S.”

  def nametag(first_name, last_name):
return("___.".format(___))
Asked By: Deepak Pandey

||

Answers:

You should use an fstring. It’s more modern (and easier to read) than format().
f'{first_name} {last_name[0]}.'

Answered By: David Smolinski
def nametag(first_name, last_name):
    return '{} {}.'.format(first_name, last_name[0])

will put the arguments of format in place of the brackets.

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

print(nametag("Jane", "Smith")) 
print(nametag("Francesco", "Rinaldi")) 

print(nametag("Jean-Luc", "Grand-Pierre")) 
Answered By: Meenakshi

def nametag(first_name, last_name):

return("{first_name} {last_name[0]}.".format(first_name= first_name, last_name=last_name[0]))

print(nametag("Jane", "Smith"))

Should display "Jane S."

Answered By: SSV 1

Please try this:

def nametag(first_name, last_name):

    return "{} {last_name}.".format(first_name, last_name=last_name[0])


print(nametag("Jane", "Smith"))

out put:
Jane S.
Answered By: Divya Goswami

Try this out

def nametag(first_name, last_name):
    return("{}{}{}.".format(first_name," ",last_name[0]))
Answered By: samuel kairu
def nametag(first_name, last_name):
    name = '{}.{}'.format(first_name, last_name)
    location = name.find('.')
    name = name[:location + 2].replace('.', ' ') + '.'
    return name
Answered By: Sush Nayak

Correction: Here is a very simple solution that works

def nametag(first_name, last_name):
    return("{} {:.1}.".format(first_name, last_name))
print(nametag("Jane", "Smith"))
Answered By: Ogah
def nametag(first_name, last_name):
    return("{} {}.".format(first_name, last_name[0]))

print(nametag("Jane", "Smith")) 
# Should display "Jane S." 
print(nametag("Francesco", "Rinaldi")) 
# Should display "Francesco R." 
print(nametag("Jean-Luc", "Grand-Pierre")) 
# Should display "Jean-Luc G." 
Answered By: Art b
def nametag(first_name, last_name):
return '{} {:>2s}.'.format(first_name, last_name[0])

you should use {:>2s} for the space between Jane and S
"followed by a period" in question.

Answered By: Mohamed M M

How to take the name as input from user?

Answered By: IMXSwarup
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.