How to Return a String in Python?

In this tutorial, I will explain how to return a string in Python. As a data scientist working for New York-based clients, I need to process customer feedback data stored in a database as a part of my project for New York clients. My goal is to create a Python function that processes the data and returns a well-formatted string summarizing the key details for reporting. Let us learn more about this topic.

Return a String in Python

Python provides various ways to return a string. Let us learn some important methods to achieve this task.

Read How to Use a Raw Strings in Python?

1. Direct Return Statement

This is the most easy way to return a string from a function.

def get_greeting(name):
    return f"Hello, {name}!"  # Returns a formatted string

greeting = get_greeting("Alice")
print(greeting) 

Output:

Hello, Alice!

I have executed the above example code and added the screenshot below.

Return a String in Python

This is the simplest and most commonly used way to return a string from a function.

Read How to Sort a String in Python?

2. Use Concatenation

You can return a string formed by concatenating multiple strings in Python.

def create_full_name(first_name, last_name):
    return first_name + " " + last_name  # Concatenates the strings

full_name = create_full_name("John", "Doe")
print(full_name)  

Output:

John Doe

I have executed the above example code and added the screenshot below.

How to Return a String in Python

String concatenation combines multiple strings into a single string using the + operator.

Read How to Extract a Substring Between Two Characters in Python?

3. Use String Formatting (f-strings)

Formatted strings in Python are very useful when you need to include variables or expressions inside a string.

def format_message(name, age):
    return f"My name is {name} and I am {age} years old."

message = format_message("Alice", 30)
print(message) 

Output:

My name is Alice and I am 30 years old.

I have executed the above example code and added the screenshot below.

Return a String in Python f-string

F-strings provide a concise and readable way to embed expressions inside string literals.

Read How to Fix Unterminated String Literals in Python?

4. Use the join() Method

If you have a list of strings in Python and want to return them as a single string, you can use the join() method.

def join_words(words):
    return " ".join(words)  # Joins list elements with a space

sentence = join_words(["This", "is", "a", "sentence"])
print(sentence)  # Output: This is a sentence

The join() method is used to join a sequence of strings into one string, separated by a specified delimiter.

Read How to Use Python Triple Quotes with F-Strings for Multiline Strings?

5. Use str.format() Method

In Python str.format() allows you to format strings with placeholders.

def format_with_placeholders(name, age):
    return "My name is {} and I am {} years old.".format(name, age)

message = format_with_placeholders("Bob", 40)
print(message)  # Output: My name is Bob and I am 40 years old.

The str.format() method allows the use of placeholders within a string, which can be filled dynamically.

Read How to Do Case-Insensitive String Comparisons in Python?

6. Return Multiline Strings

You can return a string that spans multiple lines using triple quotes in Python.

def get_multiline_string():
    return '''This is a 
multiline string 
example in Python.'''

result = get_multiline_string()
print(result)
# Output:
# This is a 
# multiline string 
# example in Python.

Multiline strings can be returned using triple quotes (''' or """), making them ideal for storing and returning long texts.

Read How to Find the Second Occurrence of a Substring in a Python String?

Conclusion

In this tutorial, I have explained how to return a string in Python. I explained direct return statements, using concatenation, string formatting, join() method, str.format() method, and return multiline strings.

You may also like to read:

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.