Home > Software design >  Syntax question, how can I add a string to an argument in a function?
Syntax question, how can I add a string to an argument in a function?

Time:09-22

I am trying to add a period after the end of the 'names' argument I have in my function. I would like the output to be 'Hello Henry. How are you?' Is there a way that I can add one? I would like it to be built into the function itself so that any name that goes in will have period after.

def greet(name, message):
    print('Hello', name, message)

greet('Henry', 'How are you?')
greet(name='Jill', message='Sup?')

CodePudding user response:

Use format for cleaner outputs

def greet(name, message):
    print("Hello {}. {}".format(name, message))

Makes it more readable

CodePudding user response:

Another way to achieve it using python f strings:

def greet(name, message):
    print(f'Hello {name}. {message}')

greet('Henry', 'How are you?')
greet(name='Jill', message='Sup?')

Output:

Hello Henry. How are you?
Hello Jill. Sup?

CodePudding user response:

You should write greet() as follows:

def greet(name, message):
    print('Hello', name   '.', message)
  • Related