Skip to content

How to return a string in Python | Example code

  • by

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

def fun():
    statements
    .
    .
    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


print(test_return())

Output:

How to return a string in Python

Another example with parameter

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


print(result(50))

Output: FAIL

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

Note: IDE: PyCharm 2021.3.3 (Community Edition)

Windows 10

Python 3.10.1

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

Leave a Reply

Discover more from Tutorial

Subscribe now to keep reading and get access to the full archive.

Continue reading