×

Python Tutorial

Python Basics

Python I/O

Python Operators

Python Conditions & Controls

Python Functions

Python Strings

Python Modules

Python Lists

Python OOPs

Python Arrays

Python Dictionary

Python Sets

Python Tuples

Python Exception Handling

Python NumPy

Python Pandas

Python File Handling

Python WebSocket

Python GUI Programming

Python Image Processing

Python Miscellaneous

Python Practice

Python Programs

Print a tuple with string formatting in Python

By IncludeHelp Last updated : November 26, 2023

Problem statement

Given a tuple, you have to write a Python program to print it with string formatting.

Printing a tuple with string formatting

To print a tuple with string formatting, use the .format() method to insert the tuple into the string. Write the string along with the index number as 0 within the curly braces {} and pass the tuple object in the .format() method.

Syntax

Below is the syntax:

print("string {0}".format(tuple_object))

Example

In this example, we have a tuple "city" with some values and we will print the tuple "tpl" with the string.

# defining a tuple 
cities = ("New Delhi", "Indore", "Mumbai")
 
# printing tuple with string
print("Popular cities in India are {0}".format(cities))

Output

The output of the above example is:

Popular cities in India are ('New Delhi', 'Indore', 'Mumbai')

Printing multiple tuples with string formatting

Similarly, if you have multiple tuples and want to print them with string formatting, use the index numbers with curly braces {0}, {1}, and so on … and pass the tuples objects in the .format() method and add their values in the string.

Syntax

Below is the syntax:

print("string {0} string {1}".format(tuple_object1, tuple_object2))

Example

In this example, we have two tuples "countries", and "capitals" and we will print their values with the string.

# defining two tuples 
countries = ("India", "USA", "UK")
capitals = ("New Delhi", "Washington, D.C.", "London")
 
# printing tuple with string
print("Countries are: {0} \n and their capitals are: {1}".format(countries, capitals))

Output

The output of the above example is:

Countries are: ('India', 'USA', 'UK') 
 and their capitals are: ('New Delhi', 'Washington, D.C.', 'London')

Printing nested tuples with string formatting

Nested tuples can also be added and printed with a string using the .format() method.

Syntax

Below is the syntax:

print("string {0}".format(tuple_object)

Example

In this example, we have nested two "students" and we will print their values with the string.

# Creating a nested tupls
students = ((1, "Alex", 21), (2, "Alvin", 23))
 
# printing nested tuple with string
print("Students data {0}".format(students))

Output

The output of the above example is:

Students data ((1, 'Alex', 21), (2, 'Alvin', 23))

Python Tuple Programs »

Advertisement
Advertisement

Comments and Discussions!

Load comments ↻


Advertisement
Advertisement
Advertisement

Copyright © 2025 www.includehelp.com. All rights reserved.