Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

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

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"))
over 4 years ago · Santiago Trujillo
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!