What does %s mean in a Python format string? - GeeksforGeeks (2024)

Last Updated : 29 Oct, 2022

Summarize

Comments

Improve

The % symbol is used in Python with a large variety of data types and configurations. %s specifically is used to perform concatenation of strings together. It allows us to format a value inside a string. It is used to incorporate another string within a string. It automatically provides type conversion from value to string.

The %s operator is put where the string is to be specified. The number of values you want to append to a string should be equivalent to the number specified in parentheses after the % operator at the end of the string value.

The following Python code illustrates the way of performing string formatting.

Simple use of %s

Python3

# declaring a string variable

name = "Geek"

# append a string within a string

print("Hey, %s!" % name)

Output

Hey, Geek!

Multiple %s

Multiple strings can also be appended within a single string using the %s operator. The strings are replaced in the order of their position in the brackets, wherever there is an %s sign. This is illustrated using the following code snippet :

Python3

# declaring a string variable

var1 = "Geek!"

var2 = "Geeks for Geeks"

# append multiple strings within a string

print("Hello %s Are you enjoying being at %s for preparations." % (var1, var2))

Output

Hello Geek! Are you enjoying being at Geeks for Geeks for preparations.

Mapping strings to %s

However, the number of occurrences of this operator must be equal to the number of strings to replace with after the % sign. Otherwise, an error of the type “TypeError: not enough arguments for format string” is thrown.

Python3

# declaring string variables

str1 = 'Understanding'

str2 = '%s'

str3 = 'at'

str4 = 'GeeksforGeeks'

# concatenating strings but %s not equal to string variables

final_str = "%s %s %s %s" % (str1, str3, str4)

# printing the final string

print("Concatenating multiple strings using Python '%s' operator:\n")

print(final_str)

Error

Traceback (most recent call last):

File “/home/c7b65fabd2ad00163eba70bbc39685d3.py”, line 8, in <module>

final_str = “%s %s %s %s” % (str1, str3, str4)

TypeError: not enough arguments for format string

Correct Code

Python3

# declaring string variables

str1 = 'Understanding'

str2 = '%s'

str3 = 'at'

str4 = 'GeeksforGeeks'

# concatenating strings

final_str = "%s %s %s %s" % (str1, str2, str3, str4)

# printing the final string

print("Concatenating multiple strings using Python '%s' operator:\n")

print(final_str)

Output

Concatenating multiple strings using Python '%s' operator:Understanding %s at GeeksforGeeks

Order %s using dictionary

The strings are printed in whatever order they are appended using the dictionary key in output.

Python3

# declaring string variables with dictionary

dct = {'str1': 'at',

'str2': 'GeeksforGeeks',

'str3': 'Understanding',

'str4': '%s'}

# concatenating strings

final_str = "%(str3)s %(str4)s %(str1)s %(str2)s" % dct

# printing the final string

print("Concatenating multiple strings using Python '%s' operator:\n")

print(final_str)

Output

Concatenating multiple strings using Python '%s' operator:Understanding %s at GeeksforGeeks

List as a string for %s

A non-string operator can also be formatted using the %s symbol in Python. Tuples can also be both inserted and formatted using this operator.

Python3

# declaring string variables

str1 = 'Understanding'

str2 = 'integers'

str3 = 'at'

str4 = 'GeeksforGeeks = '

# declaring list variables

lst = [1, 2, 3]

# concatenating strings as well as list

final_str = "%s %s %s %s %s" % (str1, str2, str3, str4, lst)

# printing the final string

print("Concatenating multiple values using Python '%s' operator:\n")

print(final_str)

Output

Concatenating multiple values using Python '%s' operator:Understanding integers at GeeksforGeeks = [1, 2, 3]


Please Login to comment...

What does %s mean in a Python format string? - GeeksforGeeks (2024)
Top Articles
What does it mean if my credit score dips?
Bitcoin Price Prediction: No Confirmation of Breakout or Bloodbath, Price Drops Below $66K
Great Clips Mount Airy Nc
The Largest Banks - ​​How to Transfer Money With Only Card Number and CVV (2024)
Breaded Mushrooms
Ymca Sammamish Class Schedule
Fort Carson Cif Phone Number
Ofw Pinoy Channel Su
Songkick Detroit
Bloxburg Image Ids
Bustle Daily Horoscope
Weather Annapolis 10 Day
Detroit Lions 50 50
Tokioof
What Is Njvpdi
How do you like playing as an antagonist? - Goonstation Forums
7440 Dean Martin Dr Suite 204 Directions
How to find cash from balance sheet?
Cvb Location Code Lookup
Bcbs Prefix List Phone Numbers
Canvas Nthurston
Sea To Dallas Google Flights
The Old Way Showtimes Near Regency Theatres Granada Hills
Magic Seaweed Daytona
Southland Goldendoodles
Student Portal Stvt
2015 Kia Soul Serpentine Belt Diagram
O'reilly's In Mathis Texas
Vera Bradley Factory Outlet Sunbury Products
The Goonies Showtimes Near Marcus Rosemount Cinema
49S Results Coral
Best New England Boarding Schools
Ripsi Terzian Instagram
Melissa N. Comics
Blackstone Launchpad Ucf
Sitting Human Silhouette Demonologist
Watchdocumentaries Gun Mayhem 2
Lichen - 1.17.0 - Gemsbok! Antler Windchimes! Shoji Screens!
Hair Love Salon Bradley Beach
The Bold And The Beautiful Recaps Soap Central
AI-Powered Free Online Flashcards for Studying | Kahoot!
19 Best Seafood Restaurants in San Antonio - The Texas Tasty
Boone County Sheriff 700 Report
Winco Money Order Hours
Www Usps Com Passport Scheduler
Callie Gullickson Eye Patches
Karen Wilson Facebook
Canvas Elms Umd
Horseneck Beach State Reservation Water Temperature
Minute Clinic Mooresville Nc
15:30 Est
Latest Posts
Article information

Author: Saturnina Altenwerth DVM

Last Updated:

Views: 5378

Rating: 4.3 / 5 (64 voted)

Reviews: 95% of readers found this page helpful

Author information

Name: Saturnina Altenwerth DVM

Birthday: 1992-08-21

Address: Apt. 237 662 Haag Mills, East Verenaport, MO 57071-5493

Phone: +331850833384

Job: District Real-Estate Architect

Hobby: Skateboarding, Taxidermy, Air sports, Painting, Knife making, Letterboxing, Inline skating

Introduction: My name is Saturnina Altenwerth DVM, I am a witty, perfect, combative, beautiful, determined, fancy, determined person who loves writing and wants to share my knowledge and understanding with you.