In Python interpreter, return without '
In the Python interactive prompt, if you return a string, it will be displayed with quotes around it, mainly so that you know it's a string.
If you just print the string, it will not be shown with quotes (unless the string has quotes in it).
>>> 1 # just a number, so no quotes
1
>>> "hi" # just a string, displayed with quotes
'hi'
>>> print("hi") # being *printed* to the screen, so do not show quotes
hi
>>> "'hello'" # string with embedded single quotes
"'hello'"
>>> print("'hello'") # *printing* a string with embedded single quotes
'hello'
If you actually do need to remove leading/trailing quotation marks, use the .strip
method of the string to remove single and/or double quotes:
>>> print("""'"hello"'""")
'"hello"'
>>> print("""'"hello"'""".strip('"\''))
hello
How to return something without quotation marks when using format()
This is because you are seeing the output directly from the console, and the variable is a string so python adds the quotation marks to let you know.
To remove them:
print(convert(10))
How is return statement different in pycharm and python interpreter?
When you enter an expression in the Python REPL, it evaluates the expression and tries to print out the repr()
of the result. repr()
calls your object's __repr__()
, checks that return value is a string and throws the error.
Outside the REPL (e.g. when you run it in PyCharm) the expression result is simply discarded, repr()
is not called so no error is raised.
Return does not output any value, however, print does. Python interpreter problem?
return
does not print to output, it just returns function result. You may have seen this printing when using python shell
as it does print result for some needed reasons.
For your 1st function to print you must print the called function like this
print(x1())
How to elegantly ignore some return values of a Python function?
The standard way to show this is to assign the results you don't want to _
. For instance, if a function returns two values but you only want the first, do this:
value, _ = myfunction(param)
Most Python linters will recognize the use of _
and not complain about unused variables.
If you want to ignore all returns, then simply don't assign to anything; Python doesn't do anything with the result unless you tell it to. Printing the result is a feature in the Python shell only.
Why does typing _ in the Python interpreter return True?
_
will be the result of the last evaluated command - at interpreter start up there isn't any so you'll get a NameError
... after that, you'll get the previous result... Try opening a new interpreter and doing 2 + 2
... you'll see 4
returned, then type _
... eg:
>>> _
Traceback (most recent call last):
File "<pyshell#18>", line 1, in <module>
_
NameError: name '_' is not defined
>>> 2 + 2
4
>>> _
4
What is the best way to exit a function (which has no return value) in python before the function ends (e.g. a check fails)?
You could simply use
return
which does exactly the same as
return None
Your function will also return None
if execution reaches the end of the function body without hitting a return
statement. Returning nothing is the same as returning None
in Python.
how can i remove ' from my output in python
msg1 = " Facebook already uses AI to Filter Fake stories from the feeds of users"
output = str(msg1.split()).replace("'", "")
print(output)
Related Topics
Concatenating Two One-Dimensional Numpy Arrays
Convert Column to Date Format (Pandas Dataframe)
Force Python to Forego Native SQLite3 and Use the (Installed) Latest SQLite3 Version
Get Human Readable Version of File Size
In Python, How to Capture the Stdout from a C++ Shared Library to a Variable
Passing a Matplotlib Figure to HTML (Flask)
Matplotlib: Specify Format of Floats for Tick Labels
Using Beautifulsoup to Extract Text Without Tags
Calling the "Source" Command from Subprocess.Popen
Is This Bad Programming Practice in Tkinter
How to Rotate the Sprite and Shoot the Bullets Towards the Mouse Position
How to Check If Any Value Is Nan in a Pandas Dataframe
Variable Assignment and Modification (In Python)
Why Does Python Assignment Not Return a Value
How to Filter Lines on Load in Pandas Read_CSV Function
How to Copy a 2D Array into a 3Rd Dimension, N Times
How to Override the Copy/Deepcopy Operations for a Python Object