Ben
Ben

Reputation: 65

How to transform capitalize name in Python?

I would like to have the name in print("\nThank you, " + name + ".") where I can put in however and whatever I want, with either lowercase and uppercase. I tried adding both .lower() and .upper() at the end of that and as well after name, but it didn't work. Any solutions? Example down below.

def welcomeGame():
   name=input("Hello! Welcome to my game. What is your first name? ")

   while True:
      if name.istitle() and name.isalpha():
         break

      print("\nI'm sorry " + name + ", names are only allowed to contain letters, and with the first capitalized letter of your name.")
      name=input("Please re-enter your name: ")

   print("\nThank you, " + name + ".")
   print("\nYou will start off with 0 points, let's play!\n")  
welcomeGame()

How I want it to look.

Hello! Welcome to my game. What is your first name? bEn
I'm sorry bEn, names are only allowed to contain letters, and with the first capitalized letter of your name.
Please re-enter your name: bEN

# the actual output after it asks to re-enter your name:
I'm sorry bEN, names are only allowed to contain letters, and with the first capitalized letter of your name. 
Please re-enter your name.

# how I actually want the output to look like after it asks to re-enter your name:
Thank you, Ben.

Upvotes: 0

Views: 92

Answers (1)

Abishek VK
Abishek VK

Reputation: 524

Use title() function.

def welcomeGame():
   name=input("Hello! Welcome to my game. What is your first name? ")

   while True:
      if name.istitle() and name.isalpha():
         break

      print("\nI'm sorry " + name + ", names are only allowed to contain letters, and 
with the first capitalized letter of your name.")
      name=input("Please re-enter your name: ").title()

   print("\nThank you, " + name + ".")
   print("\nYou will start off with 0 points, let's play!\n")  
welcomeGame()

Upvotes: 1

Related Questions