Stack Overflow
stackoverflow.com › questions › 56484258 › returning-a-string-in-python
Returning a string in python - Stack Overflow
the caller script just need to print the return string. It's just a print statement: Copyprint utils.test_return · Actual result: <function test_return at 0x7f9cff9d8a28> Expected result: nice · python · Share · Share a link to this question · Copy linkCC BY-SA 4.0 ·
Reddit
reddit.com › r/learnpython › function returning a string
r/learnpython on Reddit: Function returning a string
January 30, 2022 -
Hello,
I have a simple question about returning a string from a python function.
Say we have this simple funciton.
def min_to_hour(x):
hours=x/60
return (x , 'minutes', 'is' , hours , 'hours')
print(min_to_hour(60))
The results I get is (60, 'minutes', 'is', 1.0, 'hours')
However, I need the program to output only: 60 minutes is 1 hour.
How can I do this?
Top answer 1 of 5
7
You are returning a tuple, not a string. If you want to return a string, build one using string formatting. My preferred method is with f-strings. You would return f'{x} minutes is {hours} hours', where the variables are passed to the string in the curly brackets.
2 of 5
2
Look up string formatting. For instance: return "%s minutes is %s hours." % (x, hours) You can also build an output string with +, but it’s not a fun experience if you are trying to stick non-strings in it: return str(x) + " minutes is " + str(hours) + " hours."
How to write a function that returns a simple string value
Help please. I have to write a simple function that returns a string. The string (in this example) is a city and a country e.g. London, England I have done some other exercises where I return a formatted name e.g. Bob Johnson, but I cannot get this one right Here is my codes so far: def ... More on discuss.python.org
For a function, how to make the function result return (String)
If you look in your elif statement, you wrote String with a capital "S", but the parameter's name is "string" with a lowercase "s". Also, you'll need to capitalize the "Y" in "Your" at the beginning of both strings you're returning. That's not Python caring about case there, because it's in ... More on teamtreehouse.com
Why return a string?
I think you understanding the return ... for returning strings. For code sharing and other "Markdown" in your post, checkout the "Markdown Cheatsheet" link under the textarea. When I want to insert code, like the example below, Use 3 lines. Line 1 - Three backtics (- key to the left of the 1 and above the tab on a standard us keyboard) and language (like python) Line 2 - ... More on teamtreehouse.com
Python: can a function return a string? - Stack Overflow
I am making a recursive function that slices string until it is empty. When it is empty it alternatively selects the characters and is supposed to print or return the value. In this case I am expec... More on stackoverflow.com
Videos
08:17
🐍 Python Tutorial #22: the function return statement - YouTube
How to Return a Clean String with Variables in Python ...
01:58
How to Return String from Main Function in Python using Subprocess ...
11:49
Python Return Return Vs Print() (Visual Explanation) | #Python ...
06:22
Returning Values | Python Programming Ep. 22 - YouTube
Python 20: A function that takes an argument and return a ...
DigitalOcean
digitalocean.com › community › tutorials › python-return-statement
Python return statement | DigitalOcean
August 3, 2022 - Python Return Boolean · Let’s look at an example where our function will return the string representation of the argument. We can use the str() function to get the string representation of an object.
Python.org
discuss.python.org › python help
How to write a function that returns a simple string value - Python Help - Discussions on Python.org
October 11, 2022 - Help please. I have to write a simple function that returns a string. The string (in this example) is a city and a country e.g. London, England I have done some other exercises where I return a formatted name e.g. Bob Johnson, but I cannot get this one right Here is my codes so far: def city_country(city_name, country_name): """Return a string value of information about a city and country.""" location = f"{city_name} {country_name}" return location location = city_country('Santiago', 'Chi...
Team Treehouse
teamtreehouse.com › community › for-a-function-how-to-make-the-function-result-return-string
For a function, how to make the function result return (String) (Example) | Treehouse Community
August 9, 2017 - def just_right(string): if len(string)<5: return ("your string is too short") elif len(String)>5: return ("your string is too long") else: return True ... You have a small typo in your elif statement. Remember that Python is case-sensitive, so it matters whether you type a capital or lowercase letter in all of your symbols.
W3Schools
w3schools.com › python › python_ref_string.asp
Python String Methods
Python has a set of built-in methods that you can use on strings. Note: All string methods returns new values.
Python documentation
docs.python.org › 3 › library › typing.html
typing — Support for type hints
4 days ago - The Python runtime does not enforce function and variable type annotations. They can be used by third party tools such as type checkers, IDEs, linters, etc. This module provides runtime support for type hints. ... def surface_area_of_cube(edge_length: float) -> str: return f"The surface area of the cube is {6 * edge_length ** 2}."
Mimo
mimo.org › glossary › python › return
Python Return Statement - Syntax, Usage, and Examples
def validate_age(age): # validate ... True # if none of the conditions trigger, return True · When returning strings, you can join elements with commas to create formatted output....
Python documentation
docs.python.org › 3 › tutorial › inputoutput.html
7. Input and Output — Python 3.14.3 documentation
To read a file’s contents, call f.read(size), which reads some quantity of data and returns it as a string (in text mode) or bytes object (in binary mode). size is an optional numeric argument. When size is omitted or negative, the entire contents of the file will be read and returned; it’s your problem if the file is twice as large as your machine’s memory.
W3Schools
w3schools.com › python › python_howto_reverse_string.asp
How to reverse a String in Python
If you like to have a function where you can send your strings, and return them backwards, you can create a function and insert the code from the example above.
Python.org
discuss.python.org › ideas
Space between "return" and string - Ideas - Discussions on Python.org
January 22, 2024 - When a function in Python returns a string, the string can be directly connected after “return”. The following is an error example: def foo(): return"Foo" This way of writing is extremely irregular, but it can stil…