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
Collision Insurance for Rental Cars
How the value of the 20 largest college endowments changed last year
Netronline Taxes
Victor Spizzirri Linkedin
Warren Ohio Craigslist
Trevor Goodwin Obituary St Cloud
12 Rue Gotlib 21St Arrondissem*nt
Practical Magic 123Movies
Bustle Daily Horoscope
Rainfall Map Oklahoma
What’s the Difference Between Cash Flow and Profit?
Used Wood Cook Stoves For Sale Craigslist
Everything You Need to Know About Holly by Stephen King
Zürich Stadion Letzigrund detailed interactive seating plan with seat & row numbers | Sitzplan Saalplan with Sitzplatz & Reihen Nummerierung
Leeks — A Dirty Little Secret (Ingredient)
Craiglist Kpr
使用 RHEL 8 时的注意事项 | Red Hat Product Documentation
Uta Kinesiology Advising
The Blind Showtimes Near Amc Merchants Crossing 16
Stoney's Pizza & Gaming Parlor Danville Menu
67-72 Chevy Truck Parts Craigslist
Www Va Lottery Com Result
2000 Ford F-150 for sale - Scottsdale, AZ - craigslist
Margaret Shelton Jeopardy Age
10 Best Quotes From Venom (2018)
WOODSTOCK CELEBRATES 50 YEARS WITH COMPREHENSIVE 38-CD DELUXE BOXED SET | Rhino
County Cricket Championship, day one - scores, radio commentary & live text
R/Orangetheory
Donald Trump Assassination Gold Coin JD Vance USA Flag President FIGHT CIA FBI • $11.73
Emiri's Adventures
Wbli Playlist
Tenant Vs. Occupant: Is There Really A Difference Between Them?
Oreillys Federal And Evans
AsROck Q1900B ITX und Ramverträglichkeit
Build-A-Team: Putting together the best Cathedral basketball team
Toonily The Carry
World History Kazwire
Frcp 47
Metro Pcs Forest City Iowa
Differential Diagnosis
Foxxequeen
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
What is a lifetime maximum benefit? | healthinsurance.org
New Zero Turn Mowers For Sale Near Me
De boeken van Val McDermid op volgorde
Roller Znen ZN50QT-E
Diesel Technician/Mechanic III - Entry Level - transportation - job employment - craigslist
Epower Raley's
What Responsibilities Are Listed In Duties 2 3 And 4
Ranking 134 college football teams after Week 1, from Georgia to Temple
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.