paulnewbie34
paulnewbie34

Reputation: 17

Syntax question, how can I add a string to an argument in a function?

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?')

Upvotes: 0

Views: 126

Answers (3)

Bhagyesh Dudhediya
Bhagyesh Dudhediya

Reputation: 1854

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?

Upvotes: 1

zar3bski
zar3bski

Reputation: 3181

Use format for cleaner outputs

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

Makes it more readable

Upvotes: 1

La-comadreja
La-comadreja

Reputation: 5755

You should write greet() as follows:

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

Upvotes: 0

Related Questions