This question already has an answer here:
Print out all code in a Python script
(1 answer)
Closed 8 years ago.
How does one print the lines of code?
Suppose we have here two variables.
var1 = 2*8
msg = "Answer is: "
What statement should i add here so that this program will print the source code?
The easiest way to print the lines of codes is through the use of the built-in functions.
print open(__file__).read()
Or you could just write the code in string mode and simply print them. However that obviously won't be executable codes anymore once written in quotation marks.
Related
This question already has answers here:
What is the purpose of the return statement? How is it different from printing?
(15 answers)
Closed 1 year ago.
I am very new at python. I am wondering of the easiest way or alternative to defining a function as a string, and then printing said string in another print line, such as demonstrated here:
def world():
print("World!")
print("Hello " + world())
I am sure that this is an easy fix/search, but I cannot seem to find what I am searching for. Thank you!
I'm not sure what you are trying to accomplish here. But the answer you are looking for is,
def world():
return ("World!")
print("Hello " + world())
This question already has answers here:
How can I print variable and string on same line in Python? [duplicate]
(18 answers)
Closed 2 years ago.
Ok so I'm working on a problem in my python class. I've gotten most of it figured out aside from print statements. I assigned the arguements correctly (I think) and am just trying to get the text to print correctly on the terminal side. What am I doing wrong here?
here is what I currently have
is the example that mine is supposed to look similar to
you could use f string print statements too like this:
print(f"distance in knots is: {distance_in_knots}")
This question already has answers here:
How do I split the definition of a long string over multiple lines?
(30 answers)
Closed 5 years ago.
I need to print a long const sentence in a python script and we follow the "not more than 80 chars a line" rule.
I know the following would work but is there a better way to write this?
print ("This is a reaaaaaaaaaaaaaaaaaaaaaaaally long "
"sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx").format("")
or,
print ("This is a reaaaaaaaaaaaaaaaaaaaaaaaally long " +
"sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx")
Or, if one of them is preferable than the other one.
Edit:To make it more clear, the expected output is,
This is a reaaaaaaaaaaaaaaaaaaaaaaaally long sentencexxxxxxxxxxxxxxxxxxxxxxxxxxxx
I think this is a better way:
print(
"This is a reaaaaaaaaaaaaaaaaaaaaaaaally long "
"sentence"
)
Actually, you need nothing to concatenate strings.
This question already has answers here:
How can I fill out a Python string with spaces?
(14 answers)
Closed 5 years ago.
Is there a custom way of padding lines of text in python, I am using the escape characters "\t", but I wonder if there is an alternative.
for example
print('My Name is:')
print('Rambo')
print('Mambo')
Output:
.My Name is:
.....Rambo
..Mambo
Try using:
print('{:>15}'.format('My Name is:'))
Refer for examples:
PyFormat
Write a simple function for yourself.
def p(a,b):
print(" "*a + b)
p(1,"while")
This should return:" while"
This question already has answers here:
Replace console output in Python
(12 answers)
Closed 4 years ago.
I am trying to build a game.
The game will have an item called a "pulsating crystal" (I am using \033[1;31;40m] to change the items colour), I want to it to be rainbow, so it keeps changing colours, without deleting everything else in the terminal. I used print(\033c) to clear the terminal but I just want to print the last line. I am sorry if the question is unclear or repetitive, or has another answer but I couldn't find another clear answer for my problem. PS I use Linux.
I just want to print the last line.
To print a line repeatedly, just override the line ending \n by giving the keyword argument end='\r' to print().