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(___))
Answers:
You should use an fstring. It’s more modern (and easier to read) than format().
f'{first_name} {last_name[0]}.'
def nametag(first_name, last_name):
return '{} {}.'.format(first_name, last_name[0])
will put the arguments of format
in place of the brackets.
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"))
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."
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.
Try this out
def nametag(first_name, last_name):
return("{}{}{}.".format(first_name," ",last_name[0]))
def nametag(first_name, last_name):
name = '{}.{}'.format(first_name, last_name)
location = name.find('.')
name = name[:location + 2].replace('.', ' ') + '.'
return name
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"))
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."
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.
How to take the name as input from user?
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(___))
You should use an fstring. It’s more modern (and easier to read) than format().
f'{first_name} {last_name[0]}.'
def nametag(first_name, last_name):
return '{} {}.'.format(first_name, last_name[0])
will put the arguments of format
in place of the brackets.
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"))
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."
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.
Try this out
def nametag(first_name, last_name):
return("{}{}{}.".format(first_name," ",last_name[0]))
def nametag(first_name, last_name):
name = '{}.{}'.format(first_name, last_name)
location = name.find('.')
name = name[:location + 2].replace('.', ' ') + '.'
return name
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"))
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."
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.
How to take the name as input from user?