Print Only Desired Output

Viewed 39

I am creating an application form using Python and PyCharm.

The form has a part wherein the applicant turns in their first name, middle name, and surname.

The code I have tried is

firstName = input("First Name: ")
middleName = input("Middle Name: ")
surname = input("Surname: ")

applicant_full_name = (surname, ',', firstName, middleName)

print(applicant_full_name)

With the code above, program prints ('Surname', ',', 'First Name', 'M')

How do I make the program print the following without the parenthesis and the single quotation marks?

6 Answers

This (surname, ',', firstName, middleName) is a tuple, see this link to learn more about tuples, this is why it prints it like:('_','_','_')

You can solve it just by making it a string instead like this:

firstName = input("First Name: ")
middleName = input("Middle Name: ")
surname = input("Surname: ")

applicant_full_name = surname + ',' + firstName + middleName

print(applicant_full_name)

Please try this, it should work nicely for you:

applicant_full_name = surname + ', ' + firstName + ' ' + middleName

When you print print(applicant_full_name) , you are printing a tuple. The string representation of a tuple is what gets printed - hence the parenthesis.

Create a proper printable string version, by combining your values. You can use + operator, to do this, or better use the str.join with a separator like this.

applicant_full_name = " ".join((surname, ',', firstName, middleName))

You can use F strings in python, There are made for tasks like these.

firstName = input("First Name: ")
middleName = input("Middle Name: ")
surname = input("Surname: ")

applicant_full_name = f"{surname},{firstName},{middleName}"

print(applicant_full_name)
firstName = input("First Name: ")
middleName = input("Middle Name: ")
surname = input("Surname: ")
#using join method
applicant_full_name =' ,'.join([surname,firstName,middleName])
#using print
print(applicant_full_name)  
print(firstName,middleName,surname,sep=',')
#% operator for string concatenation
print('%s ,%s ,%s'%(firstName,middleName,surname))

The notion linked to what you want is called string concatenation : https://www.w3schools.com/python/gloss_python_string_concatenation.asp

The output you were getting is because applicant_full_name = (surname, ',', firstName, middleName) is a tuple. It's just an object in which you store items, in this case you stored the variable surname, the ',' string and the variables first name and middle name. Therefore when you print the output it just prints out the tuple with the corresponding items in it.

It's a bit like if you had applicant_full_name = [surname, ',', firstName, middleName]. In this case python would print the list with corresponding values in it.

Try : applicant_full_name = surname + ',' + firstName + middleName :)

Related