How to combine 2 strings from different functions in Python

Viewed 79

I'm trying to combine two user input strings from different functions and then have it print as one statement. I know to combine two regular strings (ones not in a function) I would use the '+' function, but I can't figure out how to do it with user input being from two different functions. Can anyone help? here is my code:

def get_first_name(first_name):
    first_name = str(input('what is your first name?'))
    len_firstName = 0
    while len_firstName <= 0:
            print('that is not valid, please try again.')
            first_name = str(input('what is your first name?'))
            len_firstName = len(first_name)
            print(len(first_name))
    return first_name

get_first_name(input)


def get_last_name(last_name):
    last_name = str(input('what is your last name?'))
    count_of_last_name = len(last_name)
    if count_of_last_name < 1: 
        print('There is no last name.')
    else:
        print(last_name)
    return last_name

get_last_name(input)

# This is what I am trying to do to combine the two but it doesn't work and I'm stuck:

full_name = get_first_name + get_last_name
print(full_name)
4 Answers

The main misunderstanding here seems mostly based around how functions should be structured. To get the behavior you want, define two distinct functions: get_first_name and get_last_name. These functions should accept no parameters, and instead return a string which you read from input (parameters would be necessary if you are passing data IN to the function, but your goal here is to get a name value OUT).

You can then define a get_full_name function where you call each, and combine their results. Call this function, remembering that function calls must end with parentheses. Your result may look something like this! Notice how the print statement is not inside a function definition, so it is executed when you run your program.

# define functions
def get_first_name():
  first_name = input("What is your first name? ")
  return first_name

def get_last_name():
  last_name = input("What is your last name? ")
  return last_name

def get_full_name():
  full_name = get_first_name() + " " + get_last_name()
  return full_name

# call your full_name function
print("Hello, " + get_full_name())

Let me know if any of that is unclear!

I would say Ethan answerd your question about why the functions dont work perfectly. What I'd like to add is a reworked version of your first function

def get_first_name():
    first_name = ''
    while len(first_name) == 0:
        first_name = input('what is your first name?\n')
        if len(first_name) == 0:
            print('that is not valid, please try again.')
    return first_name


print(get_first_name())

In your version you'd always get a 'not valid' message even if the input is valid.

As to why your functions don't need any attributes, maybe my silly painting might help :)

or this code:

def output(first_name):
    print('my first name is ' + first_name)

my_name = 'ovski'

output(my_name)

The (first_name) in def output is an attribute that is used inside the function.

In other words:

  • 'ovski' is stored inside the variable my_name
  • my_name is given as attribute into the function output
  • output uses the values inside the attribute and - combines it with the string 'my first name is '

function with attribute

You were almost there. I change your string line and it worked.

def get_first_name(first_name):
    first_name = str(input('what is your first name?'))
    len_firstName = 0
    while len_firstName > 0:
            print('that is not valid, please try again.')
            first_name = str(input('what is your first name?'))
            len_firstName = len(first_name)
            print(len(first_name))
    return first_name

def get_last_name(last_name):
    last_name = str(input('what is your last name?'))
    count_of_last_name = len(last_name)
    if count_of_last_name < 1: 
        print('There is no last name.')
    else:
        print(last_name)
    return last_name

# This is what I am trying to do to combine the two but it doesn't work and I'm stuck:

full_name = get_first_name(input) + " " + get_last_name(input)   

#Basically, you never stored/assigned the return values of your functions to someting

print(full_name)

If you want to prompt/validate user input when there is no input or empty, you can check if first_name and last_name is true or false with while not statement.

To avoid redundant input, you can use walrus operator := in while and if statement.

Your function does not need parameter, except if there are inputs or any variables outside the function that need to be passed in.

To combine return value of the get_first_name() and get_last_name() function into variable full_name, you can use f-string.

def get_first_name():
    while not (first_name := (input('what is your first name? '))):
        print('that is not valid, please try again.')
    return first_name


def get_last_name():
    if not (last_name := (input('what is your last name? '))):
        print('There is no last name.')
    return last_name


full_name = f'{get_first_name()} {get_last_name()}'
print(full_name)

# what is your first name? 
# that is not valid, please try again.
# what is your first name? John
# what is your last name? 
# There is no last name.
# John 
Related