1

How to change below code and remove unnecessary new line?

#"Please guess a number!"
choice = input ("Is it ")
print ("?")

Is it 10

?

So that result will look like

Is it 10?

3
  • Isn't the newline user input? Commented May 20, 2015 at 15:36
  • User input, as I know, by default ends with '\n'. I want to know is there any way to remove it Commented May 20, 2015 at 15:38
  • There is no newline here. Commented May 20, 2015 at 15:48

6 Answers 6

2

To "remove" the newline in your code snippet you would need to send cursor control commands to move the cursor back to the previous line. Any concrete solution would depend on the terminal you're using. Strictly speaking, there are no unnecessary newlines in the sample above. The user provided the newline that follows 10, not Python. I suppose you could try to rewrite the input processor so that user input isn't echoed similar to getpass.getpass().

Sign up to request clarification or add additional context in comments.

Comments

2

If someone looking for a solution, I found one-liner:

print('Is it ' + input('Please guess a number: ') + '?')
  • Firstly, with above line user input requested as

    Please guess a number:
    
  • Then user enter the value

    Please guess a number: 10
    
  • After input confirmation (Pressing Enter) next line appears as

    Is it 10?
    

Comments

1

you mean something like that?

choice = input ("enter somthing...")
print ("Is it "+str(choice)+"?")

1 Comment

this adds an extra line to stdout which i think is not what the question intended. A very similar solution is here that allows you to remove the line created by the input function
1

Very similar to the accepted answer in this question: remove last STDOUT line in Python

But here it is tailored to your solution. A little hackish admittedly, would be interested in a better way!

choice = input ("Is it ")
CURSOR_UP_ONE = '\x1b[1A'
ERASE_LINE = '\x1b[2K'
print CURSOR_UP_ONE + ERASE_LINE + 'Is it ' + str(choice) + '?'

3 Comments

NOTE: tested on osx successfully, don't have any other machines to try it on at the moment..
I'm afraid it's not working properly. The output is: <-[1A<-[2KIs it value?
This is by far the most simplest solution i could find for my case. Essentially we are creating a duplicate of the the previous line, followed by whatever we want and print it.
0

If you need it to be longer then the above one you could drag it out.

Choice = input("Number:") answer = "Is it %s ?" % choice print(answer)

I like Shohams tho, more pythonic imho

Comments

-2

I do deep you cannot.Because if the procedure is "choice = input("Is it ")",you must be to what end the enter,the line must be is new line.you can the change the express. choice = input("Please input the number:") print "Is it " + str(choice) + " ?"

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.