Hello, OnlineGDB Q&A section lets you put your programming query to fellow community users. Asking a solution for whole assignment is strictly not allowed. You may ask for help where you are stuck. Try to add as much information as possible so that fellow users can know about your problem statement easily.

In Python 3, how do you add text right after a variable in input and printing

+4 votes
asked Jul 6 by Yoong Tsun Chuen (370 points)

1 Answer

+1 vote
answered Jul 6 by Peter Minarik (34,450 points)
selected Jul 6 by Yoong Tsun Chuen
Best answer

You can get an input from the user with the input function. Notice the extra space at the end of the input prompt (message, or question if you prefer); it's purpose is to separate the question from the user input on the screen.

The input returned needs to be made sure it is a number (for our scenario), so int() is used to ensure this.

When you print out, you can either separate your output list with comas (,) or concatenate your whole output into a single string (in which case, the int has to be cast into a string first via str()). Notice that in the first case Python automatically separates the coma separated input with spaces when printed. When you concatenate strings, you have the freedom to control this the way you like it.

myVariable = int(input('How many kittens are there in the basked? '))
print ('There are', myVariable, 'little kittens in the basked.')
print ('There are ' + str(myVariable) + ' little kittens in the basked.')
commented Jul 6 by Yoong Tsun Chuen (370 points)
Thank you so much. But what if i want something for when they input there is something at the back?
commented Jul 6 by JB (100 points)
can u give an example ?
commented Jul 7 by Peter Minarik (34,450 points)
Sorry, I couldn't quite understand what you were after.

Could you please provide a few examples of what you meant?
commented Jul 7 by Yoong Tsun Chuen (370 points)
Nevermind, I don't think i need it anyore. Thanks anyways.
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.