How to return a string in Python | Example code

A return statement is executed and used at the end of the function in Python. You can return a string or Number or value of the expression following the return keyword to the caller.

def fun():
    return string_value

Note: The statements after the return statements are not executed.

How to return a string in Python Example

Simple example code. The following example function will return a string value.

def test_return():
    str1 = 'nice'
    return str1



How to return a string in Python

Another example with parameter

def result(mark):
    grade = ''
    if mark >= 75:
        grade = "PASS"
        grade = "FAIL"
    return grade


Output: FAIL

Do comment if you have any doubts and suggestions on this Python string return code.

Note: IDE: PyCharm 2021.1.3 (Community Edition)

Windows 10

Python 3.7

All Python Examples are in Python 3, so Maybe its different from python 2 or upgraded versions.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.