'how shall i call out this function using string format method?
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(___))
Solution 1:[1]
def nametag(first_name, last_name):
return '{} {}.'.format(first_name, last_name[0])
will put the arguments of format
in place of the brackets.
Solution 2:[2]
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.
Solution 3:[3]
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."
Solution 4:[4]
You should use an fstring. It's more modern (and easier to read) than format().f'{first_name} {last_name[0]}.'
Solution 5:[5]
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"))
Solution 6:[6]
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."
Solution 7:[7]
Try this out
def nametag(first_name, last_name):
return("{}{}{}.".format(first_name," ",last_name[0]))
Solution 8:[8]
def nametag(first_name, last_name):
name = '{}.{}'.format(first_name, last_name)
location = name.find('.')
name = name[:location + 2].replace('.', ' ') + '.'
return name
Solution 9:[9]
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"))
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | sn3jd3r |
Solution 2 | Divya Goswami |
Solution 3 | Art b |
Solution 4 | |
Solution 5 | 4b0 |
Solution 6 | SSV 1 |
Solution 7 | alagner |
Solution 8 | Marko Borkovi? |
Solution 9 | Ogah |