Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Python Tutorial
  • Interview Questions
  • Python Quiz
  • Python Glossary
  • Python Projects
  • Practice Python
  • Data Science With Python
  • Python Web Dev
  • DSA with Python
  • Python OOPs
Open In App
Next Article:
How to Print a Tab in Python
Next article icon

Starred Expression in Python

Last Updated : 01 Feb, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

Starred expression (*) in Python is used to unpack elements from iterables. It allows for extracting multiple values from a sequence and packing them into a list or a tuple or even collecting extra arguments in a function. It can also be used in assignments to separate elements or merge iterables.

Unpacking with * in function arguments

We can use * to capture multiple positional arguments in a function.

Python
def print_n(a, b, *args):     print(a)  # First argument     print(b)  # Second argument     print(args)  # All additional arguments collected in a tuple  print_n(1, 2, 3, 4, 5) 

Output
1 2 (3, 4, 5) 

Explanation: Function print_n() accepts two required arguments (a and b) and any additional arguments are captured as a tuple args using *args syntax

Below are multiple ways we can use (*) starred expression

Table of Content

  • Unpacking in function calls
  • Unpacking with * for variable assignment
  • Using * for merging lists
  • Using * with a generator expression

Unpacking in function calls

We can use * to unpack a list/tuple into positional arguments, and ** to unpack a dictionary into keyword arguments when calling a function.

Python
def info(name, age, city):     print(f"Name: {name}, Age: {age}, City: {city}")  a = ['Geeks', 30, 'New York'] info(*a)  # Unpacks the list into function arguments 

Output
Name: Geeks, Age: 30, City: New York 

Explanation:

  • Function info() accepts three arguments (name, age and city) and prints them in a formatted string.
  • List info is unpacked using *info which passes each element of list as separate arguments to the function resulting in Name: Geeks, Age: 30, City: New York

Unpacking with * for variable assignment

Starred expression can be used to unpack an iterable capturing multiple values into a list.

Python
a, *b, c = [1, 2, 3, 4, 5] print(a)   print(b)  print(c) 

Output
1 [2, 3, 4] 5 

Explanation: Assignment a, *b, c = [1, 2, 3, 4, 5] unpacks list so that a gets first element (1) c gets the last element (5) and b captures middle elements ([2, 3, 4]) as a list.

Using * for merging lists

We can use * in a list or tuple to merge sequences.

Python
a = [1, 2, 3]  b = [4, 5, 6]   # Use the unpacking operator * to merge the two lists into one c = [*a, *b]   print(c)  

Output
[1, 2, 3, 4, 5, 6] 

Explanation:

  • Unpacking operator * is used to unpack elements of lists a and b merging them into a new list c.
  • Result is combined list [1, 2, 3, 4, 5, 6] containing all elements from both a and b.

Using * with a generator expression

We can use * with a generator expression to collect results into a list.

Python
n = [1, 2, 3, 4, 5]   # Use map() with a lambda function to square each element. s = [*map(lambda x: x**2, n)]   print(s)  

Output
[1, 4, 9, 16, 25] 

Explanation:

  • map() function applies the lambda x: x**2 to each element in list n squaring each element.
  • Unpacking operator * is used to convert result into a list s.

Next Article
How to Print a Tab in Python

M

mishraaabcf9
Improve
Article Tags :
  • Python
  • Python Programs
  • Python list-programs
Practice Tags :
  • python

Similar Reads

  • Python | Print an Inverted Star Pattern
    An inverted star pattern involves printing a series of lines, each consisting of stars (*) that are arranged in a decreasing order. Here we are going to print inverted star patterns of desired sizes in Python Examples: 1) Below is the inverted star pattern of size n=5 (Because there are 5 horizontal
    2 min read
  • Structuring Python Programs
    In this article, you would come to know about proper structuring and formatting your python programs. Python Statements In general, the interpreter reads and executes the statements line by line i.e sequentially. Though, there are some statements that can alter this behavior like conditional stateme
    7 min read
  • Python Program to Print Hello World
    When we are just starting out with Python, one of the first programs we'll learn is the classic "Hello, World!" program. It's a simple program that displays the message "Hello, World!" on the screen. Here’s the "Hello World" program: [GFGTABS] Python print("Hello, World!") [/GFGTABS]Output
    2 min read
  • Find Length of String in Python
    In this article, we will learn how to find length of a string. Using the built-in function len() is the most efficient method. It returns the number of items in a container. [GFGTABS] Python a = "geeks" print(len(a)) [/GFGTABS]Output5 Using for loop and 'in' operatorA string can be iterate
    2 min read
  • How to Print a Tab in Python
    In Python, printing a tab space is useful when we need to format text, making it more readable or aligned. For example, when displaying data in columns, we might want to add a tab space between the values for a cleaner appearance. We can do this using simple methods like \t, the print() function or
    2 min read
  • Python - Modify Strings
    Python provides an wide range of built-in methods that make string manipulation simple and efficient. In this article, we'll explore several techniques for modifying strings in Python. Start with doing a simple string modification by changing the its case: Changing CaseOne of the simplest ways to mo
    3 min read
  • Python Data Structures Practice Problems
    Python Data Structures Practice Problems page covers essential structures, from lists and dictionaries to advanced ones like sets, heaps, and deques. These exercises help build a strong foundation for managing data efficiently and solving real-world programming challenges. Data Structures OverviewLi
    1 min read
  • Iterate over words of a String in Python
    In this article, we’ll explore different ways to iterate over the words in a string using Python. Let's start with an example to iterate over words in a Python string: [GFGTABS] Python s = "Learning Python is fun" for word in s.split(): print(word) [/GFGTABS]OutputLearning Python is fun Ex
    2 min read
  • Python String Input Output
    In Python, input and output operations are fundamental for interacting with users and displaying results. The input() function is used to gather input from the user and the print() function is used to display output. Input operations in PythonPython’s input() function allows us to get data from the
    3 min read
  • Understanding the Execution of Python Program
    This article aims at providing a detailed insight into the execution of the Python program. Let's consider the below example. Example: C/C++ Code a = 10 b = 10 print("Sum ", (a+b)) Output: Sum 20 Suppose the above python program is saved as first.py. Here first is the name and .py is the e
    2 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences