Answer a question

I know I can stop print from writing a newline by adding a comma

print "Hello, world!",

But how do I stop raw_input from writing a newline?

print "Hello, ",
name = raw_input()
print ", how do you do?"

Result:

Hello, Tomas
, how do you do?

Result I want:

Hello, Tomas, how do you do?

Answers

But how do I stop raw_input from writing a newline?

In short: You can't.

raw_input() will always echo the text entered by the user, including the trailing newline. That means whatever the user is typing will be printed to standard output.

If you want to prevent this, you will have to use a terminal control library such as the curses module. This is not portable, though -- for example, curses in not available on Windows systems.

Logo

Python社区为您提供最前沿的新闻资讯和知识内容

更多推荐