简体   繁体   中英

How would I create a new line in Python?

I am using Python 2.7 and this is what I am working with

print( "Massa: ", line.count("massa")) 
# trying to create a new line between these two lines 
print( "Lorem: ",line.count("lorem")+1) 

I tried this

print( "Massa: ", line.count("massa"))\n( "Lorem: ", line.count("lorem")+1)

and did not get the results I was looking for

If you mean that you want to print it with a single print statement, this will do it.

print "Massa: ", line.count("massa"), "\n", "Lorem: ", line.count("lorem")+1

Since you are using Python 2.7 I removed the enclosing brackets, otherwise the strings are treated as elements of a tuple. I have also added a new line character \\n in the middle to separate the output into 2 lines.

If you print the itmes as 2 separate print statements a new line will appear between them:

print "Massa: ", line.count("massa")
print "Lorem: ", line.count("lorem")+1

I think you can just use:

print ""

to print a newline

\\n creates a line break. You can pass it as a single string and use format to place your parameters.

print("Massa: {0}\nLorem: {1}".format(line.count("massa"), line.count("lorem")+1))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM