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.

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

+7 votes
asked Jul 6, 2021 by Yoong Tsun Chuen (550 points)
closed Feb 21, 2022 by Admin
closed with the note: answered

2 Answers

+2 votes
answered Jul 6, 2021 by Peter Minarik (89,040 points)
selected Jul 6, 2021 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, 2021 by Yoong Tsun Chuen (550 points)
Thank you so much. But what if i want something for when they input there is something at the back?
commented Jul 6, 2021 by JB (100 points)
can u give an example ?
commented Jul 7, 2021 by Peter Minarik (89,040 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, 2021 by Yoong Tsun Chuen (550 points)
Nevermind, I don't think i need it anyore. Thanks anyways.
0 votes
answered Dec 11, 2021 by ManavPatni (200 points)

See you can do it by many ways, but I will suggest you two methods to do this this two methods are very easy and basic in python programing.

So, the first method is to add text with input function it self:

       my_input = input("Enter your name here:")

Another method to do this is:

        print(input("Enter your name:")\n "fast)

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.