The White Wolf
The White Wolf

Reputation: 65

How to concatenate two functions on the same line?

def headName():
    print (Name[0].upper())
def tailName():
    print (Name[1:].lower())

Name = input("Please enter a name ")
headName()
tailName()

That's my code; I want to know how to concatinate headName() and tailName(), so that they're on the same line. Thanks

Upvotes: 1

Views: 3639

Answers (4)

user4785313
user4785313

Reputation:

You can also try:

def headName():
    print ((Name[0].upper()), end="")

This will cause your print function to end with nothing, instead of ending with a newline (default).

For more information: https://docs.python.org/3/whatsnew/3.0.html

Upvotes: 1

Jose Haro Peralta
Jose Haro Peralta

Reputation: 999

You can also use string formatting, which in case that you wanted to customize further the output would give you more control over the outcome:

def headName():
    return Name[0].upper()
def tailName():
    return Name[1:].lower()

Name = input("Please enter a name ")
print('{}{}'.format(headName(), tailName()))

Upvotes: 1

Paul Evans
Paul Evans

Reputation: 27577

To print on the same line call them in one print statement, something like:

print(headName(), ' ', tailName())

Upvotes: 1

BrenBarn
BrenBarn

Reputation: 251373

You can't do that without rewriting the functions. The newline is added by print. Since you call print inside the functions, nothing you do outside the function can undo the newline that was already added inside.

A better idea is to have your functions return the values, and then do the printing outside:

def headName():
    return Name[0].upper()
def tailName():
    return Name[1:].lower()

Name = input("Please enter a name ")
print(headName(), tailName(), sep="")

Incidentally, what you are doing can also be accomplished directly with Name.title().

Upvotes: 4

Related Questions